;define(function() {
	var count = 0;
	$.extend(Array.prototype, {
		indexOf: function(o) {
			for(var i = 0, len = this.length; i < len; i++) {
				if(this[i] == o) {
					return i;
				}
			}
			return -1;
		},
		remove: function(o) {
			var index = this.indexOf(o);
			if(index != -1) {
				this.splice(index, 1);
			}
			return this;
		},
		removeById: function(filed, id) {
			for(var i = 0, len = this.length; i < len; i++) {
				if(this[i][filed] == id) {
					this.splice(i, 1);
					return this;
				}
			}
			return this;
		}
	});

	//移除对象
	function removeNode(panel) {
		panel.each(function() {
			$(this).remove();
			if($.browser.msie) {
				this.outerHTML = "";
			}
		});
	};

	function _fit(jq, fit) {
		fit = fit == undefined ? true : fit;
		var t = jq[0];
		var p = (t.tagName == "BODY" ? t : jq.parent()[0]);
		var _11 = p.fcount || 0;
		if(fit) {
			if(!t.fitted) {
				t.fitted = true;
				p.fcount = _11 + 1;
				$(p).addClass("nrietpanel-noscroll");
				if(p.tagName == "BODY") {
					$("html").addClass("nrietpanel-fit");
				}
			}
		} else {
			if(t.fitted) {
				t.fitted = false;
				p.fcount = _11 - 1;
				if(p.fcount == 0) {
					$(p).removeClass("nrietpanel-noscroll");
					if(p.tagName == "BODY") {
						$("html").removeClass("nrietpanel-fit");
					}
				}
			}
		}
		return { width: $(p).width(), height: $(p).height() };
	};

	function _outerWidth(jq, _e) {
		if(_e == undefined) {
			if(jq[0] == window) {
				return jq.width() || document.body.clientWidth;
			}
			return jq.outerWidth() || 0;
		}
		return jq.each(function() {
			if($._boxModel) {
				$(this).css("width", _e - ($(this).outerWidth() - $(this).width()));
			} else {
				$(this).css("width", _e);
			}
		});
	};

	function _outerHeight(jq, _f) {
		if(_f == undefined) {
			if(jq[0] == window) {
				return jq.height() || document.body.clientHeight;
			}
			return jq.outerHeight() || 0;
		}
		return jq.each(function() {
			if($._boxModel) {
				$(this).height(_f - ($(this).outerHeight() - $(this).height()));
			} else {
				$(this).height(_f);
			}
		});
	};

	/**  panel part start */
	//渲染面板
	function wrapPanel(target) {
		$(target).addClass("nrietpanel-body");
		var panel = $("<div class=\"nrietpanel\"></div>").insertBefore(target);
		panel[0].appendChild(target);
		panel.bind("_resize", function() {
			var opts = $.data(target, "nrietpanel").options;
			if(opts.fit == true) {
				_resizePanel(target);
			}
			return false;
		});
		return panel;
	};

	//添加头部设置
	function addPanelHeader(jq) {
		var opts = $.data(jq, "nrietpanel").options;
		var panel = $.data(jq, "nrietpanel").panel;
		if(opts.tools && typeof opts.tools == "string") {
			panel.find(">div.nrietpanel-header>div.nrietpanel-tool .nrietpanel-tool-a").appendTo(opts.tools);
		}
		removeNode(panel.children("div.nrietpanel-header"));
		if(opts.title && !opts.noheader) {
			var header = $("<div class=\"nrietpanel-header\"><div class=\"nrietpanel-title\">" + opts.title + "</div></div>").prependTo(panel);
			if(opts.iconCls) {
				header.find(".nrietpanel-title").addClass("nrietpanel-with-icon");
				$("<div class=\"nrietpanel-icon\"></div>").addClass(opts.iconCls).appendTo(header);
			}
			var tool = $("<div class=\"nrietpanel-tool\"></div>").appendTo(header);
			tool.bind("click", function(e) {
				e.stopPropagation();
			});
			if(opts.tools) {
				if(typeof opts.tools == "string") {
					$(opts.tools).children().each(function() {
						$(this).addClass($(this).attr("iconCls")).addClass("nrietpanel-tool-a").appendTo(tool);
					});
				} else {
					for(var i = 0; i < opts.tools.length; i++) {
						var t = $("<a href=\"javascript:void(0)\"></a>").addClass(opts.tools[i].iconCls).appendTo(tool);
						if(opts.tools[i].handler) {
							t.bind("click", eval(opts.tools[i].handler));
						}
					}
				}
			}
			if(opts.collapsible) {
				$("<a class=\"nrietpanel-tool-collapse\" href=\"javascript:void(0)\"></a>").appendTo(tool).bind("click", function() {
					if(opts.collapsed == true) {
						expandPanel(jq, true);
					} else {
						_collapse(jq, true);
					}
					return false;
				});
			}
			if(opts.minimizable) {
				$("<a class=\"nrietpanel-tool-min\" href=\"javascript:void(0)\"></a>").appendTo(tool).bind("click", function() {
					_minimize(jq);
					return false;
				});
			}
			if(opts.maximizable) {
				$("<a class=\"nrietpanel-tool-max\" href=\"javascript:void(0)\"></a>").appendTo(tool).bind("click", function() {
					if(opts.maximized == true) {
						_restore(jq);
					} else {
						_maximize(jq);
					}
					return false;
				});
			}
			if(opts.closable) {
				$("<a class=\"nrietpanel-tool-close\" href=\"javascript:void(0)\"></a>").appendTo(tool).bind("click", function() {
					_close(jq);
					return false;
				});
			}
			panel.children("div.nrietpanel-body").removeClass("nrietpanel-body-noheader");
		} else {
			panel.children("div.nrietpanel-body").addClass("nrietpanel-body-noheader");
		}
	};

	//设置边框
	function setPanelBorder(jq) {
		var opts = $.data(jq, "nrietpanel").options;
		var panel = $.data(jq, "nrietpanel").panel;
		var header = panel.find(">div.nrietpanel-header");
		var pbody = panel.find(">div.nrietpanel-body");
		panel.css(opts.style);
		panel.addClass(opts.cls);
		if(opts.border) {
			header.removeClass("nrietpanel-header-noborder");
			pbody.removeClass("nrietpanel-body-noborder");
		} else {
			header.addClass("nrietpanel-header-noborder");
			pbody.addClass("nrietpanel-body-noborder");
		}
		header.addClass(opts.headerCls);
		pbody.addClass(opts.bodyCls);
		if(opts.id) {
			$(jq).attr("id", opts.id);
		} else {
			$(jq).attr("id", "");
		}
	};

	//设置面板大小和布局
	function _resizePanel(jq, parm) {
		var options = $.data(jq, "nrietpanel").options;
		var panel = $.data(jq, "nrietpanel").panel;
		var header = panel.children("div.nrietpanel-header");
		var pbody = panel.children("div.nrietpanel-body");
		if(parm) {
			if(parm.width) {
				options.width = parm.width;
			}
			if(parm.height) {
				options.height = parm.height;
			}
			if(parm.left != null) {
				options.left = parm.left;
			}
			if(parm.top != null) {
				options.top = parm.top;
			}
		}
		options.fit ? $.extend(options, _fit(panel)) : _fit(panel, false);
		panel.css({
			left: options.left,
			top: options.top
		});
		if(!isNaN(options.width)) {
			_outerWidth(panel, options.width);
		} else {
			panel.width("auto");
		}
		_outerWidth(header.add(pbody), panel.width());
		if(!isNaN(options.height)) {
			_outerHeight(panel, options.height);
			_outerHeight(pbody, panel.height() - _outerHeight(header));
		} else {
			pbody.height("auto");
		}
		panel.css("height", "");
		options.onResize.apply(jq, [options.width, options.height]);
		panel.find(">div.nrietpanel-body>div").triggerHandler("_resize");
	};

	//刷新面板来装载远程数据。如果'href'属性有了新配置，它将重写旧的'href'属性
	function _refreshPanel(jq) {
		var panel = $.data(jq, "nrietpanel");
		var opts = panel.options;
		if(opts.href) {
			if(!panel.isLoaded || !opts.cache) {
				panel.isLoaded = false;
				_destroyPanel2(jq); //销毁控件
				if(opts.loadingMessage) {
					$(jq).html($("<div class=\"nrietpanel-loading\"></div>").html(opts.loadingMessage));
				}
				$.ajax({
					url: opts.href,
					cache: false,
					dataType: "html",
					success: function(result) {
						init(opts.extractor.call(jq, result));
						opts.onLoad.apply(jq, arguments);
						panel.isLoaded = true;
					}
				});
			}
		} else {
			if(opts.content) {
				if(!panel.isLoaded) {
					_destroyPanel2(jq);
					init(opts.content);
					panel.isLoaded = true;
				}
			}
		}

		function init(target) {
			$(jq).html(target);
			if($.parser) {
				$.parser.parse($(jq));
			}
		};
	};

	//销毁控件
	function _destroyPanel2(target) {
		var t = $(target);
		t.find(".combo-f").each(function() {
			$(this).combo("destroy");
		});
		t.find(".m-btn").each(function() {
			$(this).menubutton("destroy");
		});
		t.find(".s-btn").each(function() {
			$(this).splitbutton("destroy");
		});
	};

	//调整
	function _resizePanel2(target) {
		$(target).find("div.nrietpanel:visible,div.accordion:visible,div.tabs-container:visible,div.layout:visible").each(function() {
			$(this).triggerHandler("_resize", [true]);
		});
	};

	//最大化面板到容器大小
	function _maximizePanel(jq) {
		var opts = $.data(jq, "nrietpanel").options;
		var panel = $.data(jq, "nrietpanel").panel;
		var tool = panel.children("div.nrietpanel-header").find("a.nrietpanel-tool-max");
		if(opts.maximized == true) {
			return;
		}
		tool.addClass("nrietpanel-tool-restore");
		if(!$.data(jq, "nrietpanel").original) {
			$.data(jq, "nrietpanel").original = {
				width: opts.width,
				height: opts.height,
				left: opts.left,
				top: opts.top,
				fit: opts.fit
			};
		}
		opts.left = 0;
		opts.top = 0;
		opts.fit = true;
		_resizePanel(jq);
		opts.minimized = false;
		opts.maximized = true;
		opts.onMaximize.call(jq);
	};

	//折叠面板主题
	function _collapsePanel(jq, animate) {
		var opts = $.data(jq, "nrietpanel").options;
		var panel = $.data(jq, "nrietpanel").panel;
		var pbody = panel.children("div.nrietpanel-body");
		var header = panel.children("div.nrietpanel-header").find("a.nrietpanel-tool-collapse");
		if(opts.collapsed == true) {
			return;
		}
		pbody.stop(true, true);
		if(opts.onBeforeCollapse.call(jq) == false) {
			return;
		}
		header.addClass("nrietpanel-tool-expand");
		if(animate == true) {
			pbody.slideUp("normal", function() {
				opts.collapsed = true;
				opts.onCollapse.call(jq);
			});
		} else {
			pbody.hide();
			opts.collapsed = true;
			opts.onCollapse.call(jq);
		}
	};

	//在'forceOpen'参数设置为true的时候，打开面板时将跳过'onBeforeOpen'回调函数
	function _openPanel(jq, forceOpen) {
		var opts = $.data(jq, "nrietpanel").options;
		var panel = $.data(jq, "nrietpanel").panel;
		if(forceOpen != true) {
			if(opts.onBeforeOpen.call(jq) == false) {
				return;
			}
		}
		panel.show();
		opts.closed = false;
		opts.minimized = false;
		var tool = panel.children("div.nrietpanel-header").find("a.nrietpanel-tool-restore");
		if(tool.length) {
			opts.maximized = true;
		}
		opts.onOpen.call(jq);
		if(opts.maximized == true) {
			opts.maximized = false;
			_maximizePanel(jq);
		}
		if(opts.collapsed == true) {
			opts.collapsed = false;
			_collapsePanel(jq);
		}
		if(!opts.collapsed) {
			_refreshPanel(jq);
			_resizePanel2(jq);
		}
	};

	function initPanel(target, settings) {
		settings = settings || {};
		return target.each(function() {
			var panel = $.data(this, "nrietpanel");
			var opts;
			if(panel) {
				opts = $.extend(panel.options, settings);
				panel.isLoaded = false;
			} else {
				opts = $.extend({}, panelDefaults, settings);
				$(this).attr("title", "");
				panel = $.data(this, "nrietpanel", { options: opts, panel: wrapPanel(this), isLoaded: false });
			}
			addPanelHeader(this);
			setPanelBorder(this);
			if(opts.doSize == true) {
				panel.panel.css("display", "block");
				_resizePanel(this);
			}
			if(opts.closed == true || opts.minimized == true) {
				panel.panel.hide();
			} else {
				_openPanel(this);
			}
		});
	}
	/**  panel part end */

	function setSize(target, param) {
		if(!$.data(target, "nrietgrid")) return;
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		if(param) {
			if(param.width) {
				opts.width = param.width;
			}
			if(param.height) {
				opts.height = param.height;
			}
		}
		if(opts.fit == true) {
			var p = $.data(pnl[0], "nrietpanel").panel.parent();
			opts.width = p.width();
			opts.height = p.height();
		}
		_resizePanel(pnl[0], { width: opts.width, height: opts.height });
	};

	function fixCellWidth(target) {
		var pnl = $.data(target, "nrietgrid").panel;
		var pnlHeader = pnl.find("div.nrietgrid-header");
		pnl.find("div.nrietgrid-body td.nrietgrid-td-merged").each(function() {
			var td = $(this);
			var colspan = td.attr("colspan") || 1;
			var tdFiled = td.attr("field");
			var findTd = pnlHeader.find("td[field=" + tdFiled + "]");
			var findTdWidth = findTd.width();
			for(var i = 1; i < colspan; i++) {
				findTd = findTd.next();
				findTdWidth += findTd.outerWidth();
			}
			var tdCell = td.children("div.nrietgrid-cell");
			if($.boxModel == true) {
				tdCell.css("width", findTdWidth - (tdCell.outerWidth() - tdCell.width()));
			} else {
				tdCell.css("width", findTdWidth);
			}
		});
	};

	function resizeEditor(target) {
		var pnl = $.data(target, "nrietgrid").panel;
		pnl.find("div.nrietgrid-editable").each(function() {
			var ed = $.data(this, "nrietgrid.editor");
			if(ed.actions.resize) {
				//            	console.log($(ed.target).width());
				ed.actions.resize(ed.target, $(this).width());
				//                console.log($(ed.target).width());
			}

		});
	};

	function getColOptions(target, filed) {
		var opts = $.data(target, "nrietgrid").options;
		if(opts.columns) {
			for(var i = 0; i < opts.columns.length; i++) {
				var colOpts = opts.columns[i];
				for(var j = 0; j < colOpts.length; j++) {
					var col = colOpts[j];
					if(col.field == filed) {
						return col;
					}
				}
			}
		}
		if(opts.frozenColumns) {
			for(var i = 0; i < opts.frozenColumns.length; i++) {
				var colOpts = opts.frozenColumns[i];
				for(var j = 0; j < colOpts.length; j++) {
					var col = colOpts[j];
					if(col.field == filed) {
						return col;
					}
				}
			}
		}
		return null;
	};

	function fixDataGridHeight(target) {
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		var pnlwidth = pnl.width();
		var pnlheight = pnl.height();
		var view = pnl.children("div.nrietgrid-view");
		var view1 = view.children("div.nrietgrid-view1");
		var view2 = view.children("div.nrietgrid-view2");
		var view1header = view1.children("div.nrietgrid-header");
		var view2header = view2.children("div.nrietgrid-header");
		var view1headerTable = view1header.find("table");
		var view2headerTable = view2header.find("table");
		view.css("width", pnlwidth);
		var view1headerInner = view1header.children("div.nrietgrid-header-inner").show();
		view1.css("width", view1headerInner.find("table").width());
		if(!opts.showHeader) {
			view1headerInner.hide();
		}
		view2.css("width", pnlwidth - view1.outerWidth());

		view1.children("div.nrietgrid-header,div.nrietgrid-body,div.nrietgrid-footer").css("width", view1.width());
		view2.children("div.nrietgrid-header,div.nrietgrid-body,div.nrietgrid-footer").css("width", view2.width());

		//view2.find('.nrietgrid-header').children('table').css("width",view2.width());
		//view2.find('.nrietgrid-body').children('table').css("width",view2.width());

		//view2headerTable.css("width", view2.width());  

		var hh;
		view1header.css("height", "");
		view2header.css("height", "");
		view1headerTable.css("height", "");
		view2headerTable.css("height", "");
		hh = Math.max(view1headerTable.height(), view2headerTable.height());
		view1headerTable.height(hh);
		view2headerTable.height(hh);
		if($.boxModel == true) {
			view1header.height(hh - (view1header.outerHeight() - view1header.height()));
			view2header.height(hh - (view2header.outerHeight() - view2header.height()));
		} else {
			view1header.height(hh);
			view2header.height(hh);
		}

		if(opts.height != "auto") {
			var bodyHeight = pnlheight - view2.children("div.nrietgrid-header").outerHeight(true) - view2.children("div.nrietgrid-footer").outerHeight(true) - pnl.children("div.nrietgrid-toolbar").outerHeight(true) - pnl.children("div.nrietgrid-pager").outerHeight(true);
			view1.children("div.nrietgrid-body").height(bodyHeight);
			view2.children("div.nrietgrid-body").height(bodyHeight);
		}
		view.height(view2.height());
		view2.css("left", view1.outerWidth());
	};

	function fixRowHeight(target, index) {
		var rows = $.data(target, "nrietgrid").data.rows;
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		var view = pnl.children("div.nrietgrid-view");
		var view1 = view.children("div.nrietgrid-view1");
		var view2 = view.children("div.nrietgrid-view2");
		if(!view1.find("div.nrietgrid-body-inner").is(":empty")) {
			if(index >= 0) {
				fixSingleRowHeight(index);
			} else {
				for(var i = 0; i < rows.length; i++) {
					fixSingleRowHeight(i);
				}
				if(opts.showFooter) {
					var footerRows = $.data(target, "nrietgrid").footer || [];
					var c1 = view1.children("div.nrietgrid-footer");
					var c2 = view2.children("div.nrietgrid-footer");
					for(var i = 0; i < footerRows.length; i++) {
						fixSingleRowHeight(i, c1, c2);
					}
				}
			}
		}
		fixDataGridHeight(target);
		if(opts.height == "auto") {
			var view1body = view1.children("div.nrietgrid-body");
			var view2body = view2.children("div.nrietgrid-body");
			var outerHeightTotal = 0;
			var tmpHeight = 0;
			view2body.children().each(function() {
				var c = $(this);
				if(c.is(":visible")) {
					outerHeightTotal += c.outerHeight();
					if(tmpHeight < c.outerWidth()) {
						tmpHeight = c.outerWidth();
					}
				}
			});
			if(tmpHeight > view2body.width()) {
				outerHeightTotal += 18;
			}
			view1body.height(outerHeightTotal);
			view2body.height(outerHeightTotal);
			view.height(view2.height());
		}
		view2.children("div.nrietgrid-body").triggerHandler("scroll");

		function fixSingleRowHeight(index, c1, c2) {
			c1 = c1 || view1;
			c2 = c2 || view2;
			var tr1 = c1.find("tr[nrietgrid-row-index=" + index + "]");
			var tr2 = c2.find("tr[nrietgrid-row-index=" + index + "]");
			tr1.css("height", "");
			tr2.css("height", "");
			var maxheight = Math.max(tr1.height(), tr2.height());
			tr1.css("height", maxheight);
			tr2.css("height", maxheight);
		};
	};

	function fixColumnSize(target, filed) {
		var pnl = $.data(target, "nrietgrid").panel;
		var bf = pnl.find("div.nrietgrid-body,div.nrietgrid-footer");
		if(filed) {
			fix(filed);
		} else {
			pnl.find("div.nrietgrid-header td[field]").each(function() {
				fix($(this).attr("field"));
			});
		}
		fixCellWidth(target);
		setTimeout(function() {
			fixRowHeight(target);
			resizeEditor(target);
		}, 0);

		function fix(_field) {
			var col = getColOptions(target, _field);
			bf.find("td[field=" + _field + "]").each(function() {
				var td = $(this);
				var _colspan = td.attr("colspan") || 1;
				if(_colspan == 1) {
					td.find("div.nrietgrid-cell").css("width", col.boxWidth);
					//td.find("div.nrietgrid-editable").css("width",col.width);  
				}
			});
		};
	};

	function _48b(view) {
		var cc = view || $("head");
		var _ss = $.data(cc[0], "ss");
		if(!_ss) {
			_ss = $.data(cc[0], "ss", { cache: {}, dirty: [] });
		}
		return {
			add: function(_48e) {
				var ss = ["<style type=\"text/css\">"];
				for(var i = 0; i < _48e.length; i++) {
					_ss.cache[_48e[i][0]] = { width: _48e[i][1] };
				}
				var _ind = 0;
				for(var s in _ss.cache) {
					var item = _ss.cache[s];
					item.index = _ind++;
					ss.push(s + "{width:" + item.width + "}");
				}
				ss.push("</style>");
				$(ss.join("\n")).appendTo(cc);
				setTimeout(function() {
					cc.children("style:not(:last)").remove();
				}, 0);
			},
			getRule: function(_490) {
				var _491 = cc.children("style:last")[0];
				var _492 = _491.styleSheet ? _491.styleSheet : (_491.sheet || document.styleSheets[document.styleSheets.length - 1]);
				var _cr = _492.cssRules || _492.rules;
				return _cr[_490];
			},
			set: function(_494, _495) {
				var item = _ss.cache[_494];
				if(item) {
					item.width = _495;
					var rule = this.getRule(item.index);
					if(rule) {
						rule.style["width"] = _495;
					}
				}
			},
			remove: function(_496) {
				var tmp = [];
				for(var s in _ss.cache) {
					if(s.indexOf(_496) == -1) {
						tmp.push([s, _ss.cache[s].width]);
					}
				}
				_ss.cache = {};
				this.add(tmp);
			},
			dirty: function(_497) {
				if(_497) {
					_ss.dirty.push(_497);
				}
			},
			clean: function() {
				for(var i = 0; i < _ss.dirty.length; i++) {
					this.remove(_ss.dirty[i]);
				}
				_ss.dirty = [];
			}
		};
	};

	function wrapGrid(target, rownumbers) {
		function getDataByCondition(conditons) {
			var trAry = [];
			$("tr", conditons).each(function() {
				var thAry = [];
				$("th", this).each(function() {
					var th = $(this);
					var col = { title: th.html(), align: th.attr("align") || "left", sortable: th.attr("sortable") == "true" || false, checkbox: th.attr("checkbox") == "true" || false };
					if(th.attr("field")) {
						col.field = th.attr("field");
					}
					if(th.attr("formatter")) {
						col.formatter = eval(th.attr("formatter"));
					}
					if(th.attr("styler")) {
						col.styler = eval(th.attr("styler"));
					}
					if(th.attr("editor")) {
						var s = $.trim(th.attr("editor"));
						if(s.substr(0, 1) == "{") {
							col.editor = eval("(" + s + ")");
						} else {
							col.editor = s;
						}
					}
					if(th.attr("rowspan")) {
						col.rowspan = parseInt(th.attr("rowspan"));
					}
					if(th.attr("colspan")) {
						col.colspan = parseInt(th.attr("colspan"));
					}
					if(th.attr("width")) {
						col.width = parseInt(th.attr("width"));
					}
					if(th.attr("hidden")) {
						col.hidden = th.attr("hidden") == "true";
					}
					if(th.attr("resizable")) {
						col.resizable = th.attr("resizable") == "true";
					}
					thAry.push(col);
				});
				trAry.push(thAry);
			});
			return trAry;
		};
		var wrapObj = $("<div class=\"nrietgrid-wrap\">" + "<div class=\"nrietgrid-view\">" + "<div class=\"nrietgrid-view1\">" + "<div class=\"nrietgrid-header\">" + "<div class=\"nrietgrid-header-inner\"></div>" + "</div>" + "<div class=\"nrietgrid-body\">" + "<div class=\"nrietgrid-body-inner\"></div>" + "</div>" + "<div class=\"nrietgrid-footer\">" + "<div class=\"nrietgrid-footer-inner\"></div>" + "</div>" + "</div>" + "<div class=\"nrietgrid-view2\">" + "<div class=\"nrietgrid-header\">" + "<div class=\"nrietgrid-header-inner\"></div>" + "</div>" + "<div class=\"nrietgrid-body\"></div>" + "<div class=\"nrietgrid-footer\">" + "<div class=\"nrietgrid-footer-inner\"></div>" + "</div>" + "</div>" + "<div class=\"nrietgrid-resize-proxy\"></div>" + "</div>" + "</div>").insertAfter(target);
		initPanel(wrapObj, { doSize: false });
		$.data(wrapObj[0], "nrietpanel").panel.addClass("nrietgrid").bind("_resize", function(e, _32) {
			var opts = $.data(target, "nrietgrid").options;
			if(opts.fit == true || _32) {
				setSize(target);
				setTimeout(function() {
					if($.data(target, "nrietgrid")) {
						fixColumnSize(target);
					}
				}, 0);
			}
			return false;
		});
		$(target).hide().appendTo(wrapObj.children("div.nrietgrid-view"));

		var dataFrozen = getDataByCondition($("thead[frozen=true]", target));
		var dataFit = getDataByCondition($("thead[frozen!=true]", target));
		var view = wrapObj.children("div.nrietgrid-view");
		var _view1 = view.children("div.nrietgrid-view1");
		var _view2 = view.children("div.nrietgrid-view2");
		var _view = wrapObj.closest("div.nrietgrid-view");

		if(!_view.length) {
			_view = view;
		}
		var ss = _48b(_view);
		return {
			panel: wrapObj,
			frozenColumns: dataFrozen,
			columns: dataFit,
			dc: {
				view: view,
				view1: _view1,
				view2: _view2,
				header1: _view1.children("div.nrietgrid-header").children("div.nrietgrid-header-inner"),
				header2: _view2.children("div.nrietgrid-header").children("div.nrietgrid-header-inner"),
				body1: _view1.children("div.nrietgrid-body").children("div.nrietgrid-body-inner"),
				body2: _view2.children("div.nrietgrid-body"),
				footer1: _view1.children("div.nrietgrid-footer").children("div.nrietgrid-footer-inner"),
				footer2: _view2.children("div.nrietgrid-footer").children("div.nrietgrid-footer-inner")
			},
			ss: ss
		};
	};

	function getColumnFields(target, frozen) {
		var opts = $.data(target, "nrietgrid").options;
		var cols = (frozen == true) ? (opts.frozenColumns || [
			[]
		]) : opts.columns;
		if(cols.length == 0) {
			return [];
		}
		var columnFields = [];

		function findLocation(index) {
			var c = 0;
			var i = 0;
			while(true) {
				if(columnFields[i] == undefined) {
					if(c == index) {
						return i;
					}
					c++;
				}
				i++;
			}
		};

		function getColumnFields2(r) {
			var ff = [];
			var c = 0;
			for(var i = 0; i < cols[r].length; i++) {
				var col = cols[r][i];
				if(col.field) {
					ff.push([c, col.field]);
				}
				c += parseInt(col.colspan || "1");
			}
			for(var i = 0; i < ff.length; i++) {
				ff[i][0] = findLocation(ff[i][0]);
			}
			for(var i = 0; i < ff.length; i++) {
				var f = ff[i];
				columnFields[f[0]] = f[1];
			}
		};
		for(var i = 0; i < cols.length; i++) {
			getColumnFields2(i);
		}
		return columnFields;
	};

	function fixColumnWidth(target) {
		var opts = $.data(target, "nrietgrid").options;
		if(!opts.fitColumns) {
			return;
		}

		var pnl = $.data(target, "nrietgrid").panel;
		//        pnl.find("tr:hidden").css({"display": "block"}).addClass("nrietgrid-hiddentr");
		var view2Header = pnl.find("div.nrietgrid-view2 div.nrietgrid-header");
		var tmpWidth = 0;
		var tmpCol;
		var coFields = getColumnFields(target, false);
		for(var i = 0; i < coFields.length; i++) {
			var col = getColOptions(target, coFields[i]);
			if(!col.hidden && !col.checkbox && col.auto) {
				col.width = view2Header.find("td[field=" + col.field + "] div.nrietgrid-cell").width() - 8;
				col.boxWidth = view2Header.find("td[field=" + col.field + "] div.nrietgrid-cell").width();
				tmpWidth += col.width;
				tmpCol = col;
			}
		}
		var view2HeaderInner = view2Header.children("div.nrietgrid-header-inner").show();
		var jzWidth = view2Header.width() - view2Header.find("table").width() - opts.scrollbarSize;
		var ratio = jzWidth / tmpWidth;
		if(!opts.showHeader) {
			view2HeaderInner.hide();
		}
		for(var i = 0; i < coFields.length; i++) {
			var col = getColOptions(target, coFields[i]);
			if(!col.hidden && !col.checkbox && col.auto) {
				var floorWidth = Math.floor(col.width * ratio);
				setCellWidth(col, floorWidth);
				jzWidth -= floorWidth;
			}
		}
		fixColumnSize(target);
		if(jzWidth) {
			if(tmpCol)
				setCellWidth(tmpCol, jzWidth);
			fixColumnSize(target, tmpCol && tmpCol.field ? tmpCol.field : '');
		}

		function setCellWidth(col, width) {
			col.width += width;
			col.boxWidth += width;
			view2Header.find("td[field=" + col.field + "] div.nrietgrid-cell").css("width", col.boxWidth);
		};
	};

	function initDatagrid(target) {
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		initPanel(pnl, $.extend({}, opts, {
			doSize: false,
			onResize: function(_40, _41) {
				setTimeout(function() {
					if($.data(target, "nrietgrid")) {
						fixDataGridHeight(target);
						fixColumnWidth(target);
						opts.onResize.call(pnl, _40, _41);
					}
				}, 0);
			},
			onExpand: function() {
				fixDataGridHeight(target);
				fixRowHeight(target);
				opts.onExpand.call(pnl);
			}
		}));
		var view = pnl.children("div.nrietgrid-view");
		var view1 = view.children("div.nrietgrid-view1");
		var view2 = view.children("div.nrietgrid-view2");
		var view1HeaderInner = view1.children("div.nrietgrid-header").children("div.nrietgrid-header-inner");
		var view2HeaderInner = view2.children("div.nrietgrid-header").children("div.nrietgrid-header-inner");

		$.data(target, "nrietgrid").rowIdPrefix = "nrietgrid-row-r" + (++count);
		$.data(target, "nrietgrid").cellClassPrefix = "nrietgrid-cell-c" + count;
		appendColumns(view1HeaderInner, opts.frozenColumns, true);
		appendColumns(view2HeaderInner, opts.columns, false);
		_4d0();
		view1HeaderInner.css("display", opts.showHeader ? "block" : "none");
		view2HeaderInner.css("display", opts.showHeader ? "block" : "none");
		view1.find("div.nrietgrid-footer-inner").css("display", opts.showFooter ? "block" : "none");
		view2.find("div.nrietgrid-footer-inner").css("display", opts.showFooter ? "block" : "none");
		if(opts.toolbar) {
			if(typeof opts.toolbar == "string") {
				$(opts.toolbar).addClass("nrietgrid-toolbar").prependTo(pnl);
				$(opts.toolbar).show();
			} else {
				$("div.nrietgrid-toolbar", pnl).remove();
				var tb = $("<div class=\"nrietgrid-toolbar\"></div>").prependTo(pnl);
				for(var i = 0; i < opts.toolbar.length; i++) {
					var btn = opts.toolbar[i];
					if(btn == "-") {
						$("<div class=\"nrietgrid-btn-separator\"></div>").appendTo(tb);
					} else {
						var lnkObj = $("<a href=\"javascript:void(0)\"></a>");
						lnkObj[0].onclick = eval(btn.handler || function() {});
						lnkObj.css("float", "left").appendTo(tb).linkbutton($.extend({}, btn, { plain: true }));
					}
				}
			}
		} else {
			$("div.nrietgrid-toolbar", pnl).remove();
		}
		$("div.nrietgrid-pager", pnl).remove();
		//分页组件pagination未开发
		//      if (opts.pagination) {  
		//          var pagerObj = $("<div class=\"nrietgrid-pager\"></div>").appendTo(pnl);  
		//          pagerObj.pagination({ pageNumber: opts.pageNumber, pageSize: opts.pageSize, pageList: opts.pageList, onSelectPage: function (_pnum, _psize) {  
		//			opts.pageNumber = _pnum;  
		//			opts.pageSize = _psize;  
		//			request(target);  
		//		}  
		//          });  
		//          opts.pageSize = pagerObj.pagination("options").pageSize;  
		//      }  
		function appendColumns(container, columns, frozen) {
			if(!columns) {
				return;
			}
			$(container).show();
			$(container).empty();
			var t = $("<table class=\"nrietgrid-htable\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tbody></tbody></table>").appendTo(container);
			for(var i = 0; i < columns.length; i++) {
				var tr = $("<tr class=\"nrietgrid-header-row\"></tr>").appendTo($("tbody", t));
				var findCol = columns[i];
				for(var j = 0; j < findCol.length; j++) {
					var col = findCol[j];
					var strHtml = "";
					var pos = [];
					if(col.rowspan) {
						strHtml += "rowspan=\"" + col.rowspan + "\" ";
					}
					if(col.colspan) {
						strHtml += "colspan=\"" + col.colspan + "\" ";
					}
					var td = $("<td " + strHtml + "></td>").appendTo(tr);
					if(col.checkbox) {
						td.attr("field", col.field);
						$("<div class=\"nrietgrid-header-check\"></div>").html("<input type=\"checkbox\"/>").appendTo(td);
					} else {
						if(col.field) {
							td.attr("field", col.field);
							td.append("<div class=\"nrietgrid-cell\"><span></span><span class=\"nrietgrid-sort-icon\"></span></div>");
							$("span", td).html(col.title);
							$("span.nrietgrid-sort-icon", td).html(" ");
							var findCell = td.find("div.nrietgrid-cell");
							if(opts.sortName == col.field) {
								findCell.addClass("nrietgrid-sort-" + opts.sortOrder);
							}
							if(col.resizable == false) {
								findCell.attr("resizable", "false");
							}
							if(col.width) {
								_outerWidth(td, col.width);
								col.boxWidth = parseInt(td[0].style.width);
								findCell.css({ "width": col.width });
								col.auto = false;
							} else {
								col.auto = true;
							}
							//                             console.log(col.field+"   "+col.auto);
							findCell.css("text-align", (col.halign || col.align || ""));
							col.cellClass = $.data(target, "nrietgrid").cellClassPrefix + "-" + col.field.replace(/[\.|\s]/g, "-");
						} else {
							$("<div class=\"nrietgrid-cell-group\"></div>").html(col.title).appendTo(td);
							var findCell = td.find("div.nrietgrid-cell-group");
							if(col.width) {
								_outerWidth(td, col.width);
								//                            	if(!isNaN(col.colspan) && col.colspan>0){
								//                            		td.width(td.width() + (col.colspan-1)*9);
								//                            	}
								col.boxWidth = parseInt(td[0].style.width);
								findCell.css({ "width": col.width });
								col.auto = false;
							} else {
								col.auto = true;
							}
							if(col.sortable) {
								findCell.append($("<span class=\"nrietgrid-sort-icon\"></span>"));
								td.attr("sortfield", col.sortfield);
							}

							findCell.css("text-align", (col.halign || col.align || ""));
						}
					}
					if(col.hidden) {
						td.hide();
					}
				}
				if(!isNaN(opts.headFilter) && i == opts.headFilter) {
					tr.css({ height: "0.01em" }).addClass("nrietgrid-filtercolumn");
					tr.find("div").css({ height: "0.01em" });
				}
			}
			if(frozen && opts.rownumbers) {
				var td = $("<td rowspan=\"" + opts.frozenColumns.length + "\"><div class=\"nrietgrid-header-rownumber\"></div></td>");
				if($("tr", t).length == 0) {
					td.wrap("<tr></tr>").parent().appendTo($("tbody", t));
				} else {
					td.prependTo($("tr:first", t));
				}
			}
		};

		function _4d0() {
			var _widthstyle = [];
			var getCols = getColumnFields(target, true).concat(getColumnFields(target, false));
			for(var i = 0; i < getCols.length; i++) {
				var col = getColOptions(target, getCols[i]);
				if(col && !col.checkbox) {
					_widthstyle.push(["." + col.cellClass, col.boxWidth ? col.boxWidth + "px" : "auto"]);
				}
			}
			$.data(target, "nrietgrid").ss.add(_widthstyle);
			$.data(target, "nrietgrid").ss.dirty($.data(target, "nrietgrid").cellSelectorPrefix);
			$.data(target, "nrietgrid").cellSelectorPrefix = "." + $.data(target, "nrietgrid").cellClassPrefix;
		};
	};

	function getData(target) {
		var data = { total: 0, rows: [] };
		var getCols = getColumnFields(target, true).concat(getColumnFields(target, false));
		$(target).find("tbody tr").each(function() {
			data.total++;
			var col = {};
			for(var i = 0; i < getCols.length; i++) {
				col[getCols[i]] = $("td:eq(" + i + ")", this).html();
			}
			data.rows.push(col);
		});
		return data;
	};

	function request(target, param) {
		var pnl = $.data(target, "nrietgrid").panel;
		var opts = $.data(target, "nrietgrid").options;
		if(param) {
			opts.queryParams = param;
		}
		if(!opts.url) {
			return;
		}
		var queryObj = $.extend({}, opts.queryParams);
		//分页组件pagination未开发		
		//      if (opts.pagination) {  
		//          $.extend(queryObj, { page: opts.pageNumber, rows: opts.pageSize });  
		//      }  
		if(opts.sortName) {
			$.extend(queryObj, { sort: opts.sortName, order: opts.sortOrder });
		}
		if(opts.onBeforeLoad.call(target, queryObj) == false) {
			return;
		}
		loading(target);
		setTimeout(function() {
			ajaxRequest();
		}, 0);

		function ajaxRequest() {
			$.ajax({
				type: opts.method,
				url: opts.url,
				data: queryObj,
				dataType: "json",
				success: function(res) {
					setTimeout(function() {
						loaded(target);
					}, 0);
					if(res && res.param) {
						loadData(target, res.param.data);
					} else {
						loadData(target, res);
					}
					setTimeout(function() {
						cacheRows(target);
					}, 0);
				},
				error: function(e) {
					setTimeout(function() {
						loaded(target);
					}, 0);
					//                console.log(e);
					if(opts.onLoadError) {
						opts.onLoadError.apply(target, arguments);
					}
				}
			});
		};
	};

	function getRowIndex(target, row) {
		var opts = $.data(target, "nrietgrid").options;
		var rows = $.data(target, "nrietgrid").data.rows;
		if(typeof row == "object") {
			return rows.indexOf(row);
		} else {
			for(var i = 0; i < rows.length; i++) {
				if(rows[i][opts.idField] == row) {
					return i;
				}
			}
			return -1;
		}
	};

	function getSelectRows(target) {
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		var data = $.data(target, "nrietgrid").data;
		if(opts.idField) {
			return $.data(target, "nrietgrid").selectedRows;
		} else {
			var rowAry = [];
			$("div.nrietgrid-view2 div.nrietgrid-body tr.nrietgrid-row-selected", pnl).each(function() {
				var row_index = parseInt($(this).attr("nrietgrid-row-index"));
				rowAry.push(data.rows[row_index]);
			});
			return rowAry;
		}
	};

	function clearSelections(target) {
		clearSelectRows(target);
		var selectRows = $.data(target, "nrietgrid").selectedRows;
		selectRows.splice(0, selectRows.length);
		if($.data(target, "nrietgrid").options.cellSelect) {
			clearSelectCells(target);
			var selectCells = $.data(target, "nrietgrid").selectedCells;
			selectCells.splice(0, selectCells.length);
		}
		if($.data(target, "nrietgrid").options.columnSelect) {
			clearSelectColumns(target);
			var selectColumns = $.data(target, "nrietgrid").selectedColumns;
			selectColumns.splice(0, selectColumns.length);
		}

	};

	function selectAll(target) {
		clearSelections(target);
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		var data = $.data(target, "nrietgrid").data;
		var selectRows = $.data(target, "nrietgrid").selectedRows;
		var rows = data.rows;
		var pnlBody = pnl.find("div.nrietgrid-body");
		$("tr", pnlBody).addClass("nrietgrid-row-selected");
		$("div.nrietgrid-cell-check input[type=checkbox]", pnlBody)._propAttr("checked", true);
		for(var i = 0; i < rows.length; i++) {
			if(opts.idField) {
				(function() {
					var row = rows[i];
					for(var i = 0; i < selectRows.length; i++) {
						if(selectRows[i][opts.idField] == row[opts.idField]) {
							return;
						}
					}
					selectRows.push(row);
				})();
			}
		}
		opts.onSelectAll.call(target, rows);
	};

	function clearSelectRows(target) {
		target = $($(".nrietgrid-resize-proxy").next("div"));// 修改为当一个页面中有几个表格时，点击当前表格内容时其他的表格编辑或选中状态不消失的问题
		if(target.length > 0){
			$.each(target, function(){
				var opts = $.data(this, "nrietgrid").options;
				var pnl = $.data(this, "nrietgrid").panel;
				var data = $.data(this, "nrietgrid").data;
				var selectRows = $.data(this, "nrietgrid").selectedRows;
				$("div.nrietgrid-body tr.nrietgrid-row-selected", pnl).removeClass("nrietgrid-row-selected");
				$("div.nrietgrid-body div.nrietgrid-cell-check input[type=checkbox]", pnl)._propAttr("checked", false);
				if(opts.idField) {
					for(var i = 0; i < data.rows.length; i++) {
						selectRows.removeById(opts.idField, data.rows[i][opts.idField]);
					}
				}
				opts.onUnselectAll.call(this, data.rows);
			});
		}
	};

	function clearSelectCells(target) {
		target = $($(".nrietgrid-resize-proxy").next("div"));// 修改为当一个页面中有几个表格时，点击当前表格内容时其他的表格编辑或选中状态不消失的问题
		if(target.length > 0){
			$.each(target, function(){
				var pnl = $.data(this, "nrietgrid").panel;
				var data = $.data(this, "nrietgrid").data;
				var pnl = $.data(this, "nrietgrid").panel;
				var selectCells = $.data(this, "nrietgrid").selectedCells;
				$("div.nrietgrid-body div.nrietgrid-cell-selected", pnl).removeClass("nrietgrid-cell-selected");
				if(selectCells) {
					for(var i = 0; i < selectCells.length; i++) {
						var rowindex = selectCells[i]["rowindex"];
						var field = selectCells[i]["field"];
						if(selectCells[i]["value"] == data.rows[rowindex][field]) {
							selectCells.remove(i);
						}
					}
				}
			});
		}
	}

	function clearSelectColumns(target) {
		target = $($(".nrietgrid-resize-proxy").next("div"));// 修改为当一个页面中有几个表格时，点击当前表格内容时其他的表格编辑或选中状态不消失的问题
		if(target.length > 0){
			$.each(target, function(){
				var pnl = $.data(this, "nrietgrid").panel;
				var data = $.data(this, "nrietgrid").data;
				var pnl = $.data(this, "nrietgrid").panel;
				var selectColumns = $.data(this, "nrietgrid").selectedColumns;
				var tds = $("div.nrietgrid-body td.nrietgrid-col-selected", pnl).removeClass("nrietgrid-col-selected")
					.removeClass("nrietgrid-col-selected-top").removeClass("nrietgrid-col-selected-bottom");
				tds.each(function() {
					$(this).prev().eq(0).removeClass("nrietgrid-col-selected-prev");
				});
				if(selectColumns) {
					for(var i = 0; i < selectColumns.length; i++) {
						selectColumns.remove(i);
					}
				}
				fixColumnSize(this);
				fixColumnWidth(this);
			});
		}
	}

	function selectColumn(target, field) {
		var pnl = $.data(target, "nrietgrid").panel;
		var opts = $.data(target, "nrietgrid").options;
		var data = $.data(target, "nrietgrid").data;
		var selectColumns = $.data(target, "nrietgrid").selectedColumns;
		var fields = getColumnFields(target, true).concat(getColumnFields(target, false));
		if(fields.indexOf(field) < 0) {
			return;
		}

		var columndata = [];
		var tds = $("div.nrietgrid-body td[field=" + field + "]", pnl);
		if(!tds.hasClass("nrietgrid-col-selected")) {
			tds.addClass("nrietgrid-col-selected");
			tds.eq(0).addClass("nrietgrid-col-selected-top");
			tds.eq(tds.length - 1).addClass("nrietgrid-col-selected-bottom");
			tds.each(function() {
				$(this).prev().eq(0).addClass("nrietgrid-col-selected-prev");
			});
			for(var i = 0; i < data.rows.length; i++) {
				columndata[i] = data.rows[i][field];
			}

			(function() {
				for(var i = 0; i < selectColumns.length; i++) {
					if(selectColumns[i].field == field) {
						return;
					}
				}
				selectColumns.push({
					field: field,
					coldata: columndata
				});
			})();

			fixColumnSize(target);
			fixColumnWidth(target);
			opts.onSelectColumn.call(target, field, columndata);
		}
	}

	function selectRow(target, index) {
		var pnl = $.data(target, "nrietgrid").panel;
		var opts = $.data(target, "nrietgrid").options;
		var data = $.data(target, "nrietgrid").data;
		var selectRows = $.data(target, "nrietgrid").selectedRows;
		if(index < 0 || index >= data.rows.length) {
			return;
		}
		if(opts.singleSelect == true) {
			clearSelections(target);
		}
		var tr = $("div.nrietgrid-body tr[nrietgrid-row-index=" + index + "]", pnl);
		if(!tr.hasClass("nrietgrid-row-selected")) {
			tr.addClass("nrietgrid-row-selected");
			var ck = $("div.nrietgrid-cell-check input[type=checkbox]", tr);
			ck.attr("checked", true);
			if(opts.idField) {
				var row = data.rows[index];
				(function() {
					for(var i = 0; i < selectRows.length; i++) {
						if(selectRows[i][opts.idField] == row[opts.idField]) {
							return;
						}
					}
					selectRows.push(row);
				})();
			}
		}
		
		opts.onSelect.call(target, index, data.rows[index]);
		var view2 = pnl.find("div.nrietgrid-view2");
		var view2Header = view2.find("div.nrietgrid-header");
		var view2HeaderH = view2Header.outerHeight();
		var view2Body = view2.find("div.nrietgrid-body");
		var top = tr.position().top - view2HeaderH;
		if(top <= 0) {
			view2Body.scrollTop(view2Body.scrollTop() + top);
		} else {
			if(top + tr.outerHeight() > view2Body.height() - 18) {
				view2Body.scrollTop(view2Body.scrollTop() + top + tr.outerHeight() - view2Body.height() + 18);
			}
		}
		
		var ck = view2Body.find("div.nrietgrid-cell-check input[type=checkbox]:checked");
		if (ck.length == data.rows.length){
			view2Header.find('input[type=checkbox]')._propAttr('checked', true);
		}
	};

	function selectCell(target, rowindex, tdfield) {
		var pnl = $.data(target, "nrietgrid").panel;
		var opts = $.data(target, "nrietgrid").options;
		var data = $.data(target, "nrietgrid").data;
		var selectedCells = $.data(target, "nrietgrid").selectedCells;
		if(rowindex < 0 || rowindex >= data.rows.length) {
			return;
		}
		if(opts.singleSelect == true) {
			clearSelections(target);
		}
		var tr = $("div.nrietgrid-body tr[nrietgrid-row-index=" + rowindex + "]", pnl);
		var td = tr.children("td[field='" + tdfield + "']");
		var findindex = tr.children("td:not(:hidden)").index(td);
		if(!td.children("div").hasClass("nrietgrid-cell-selected")) {
			if(opts.editIndex) {
				td.children("div").addClass("nrietgrid-cell-selected");
			}
			var row = data.rows[rowindex];
			(function() {
				for(var i = 0; i < selectedCells.length; i++) {
					if(selectedCells[i]["rowindex"] == rowindex && selectedCells[i]["field"] == tdfield) {
						return;
					}
				}
				selectedCells.push({
					"rowindex": rowindex,
					"field": tdfield,
					"value": row[tdfield]
				});
			})();
		}
		opts.onSelectCell.call(target, rowindex, data.rows[rowindex], tdfield);
		var view2 = pnl.find("div.nrietgrid-view2");
		var view2Header = view2.find("div.nrietgrid-header").outerHeight();
		var view2Body = view2.find("div.nrietgrid-body");
		var top = tr.position().top - view2Header;
		if(top <= 0) {
			view2Body.scrollTop(view2Body.scrollTop() + top);
		} else {
			if(top + tr.outerHeight() > view2Body.height() - 18) {
				view2Body.scrollTop(view2Body.scrollTop() + top + tr.outerHeight() - view2Body.height() + 18);
			}
		}
	}

	function selectRecord(target, id) {
		var opts = $.data(target, "nrietgrid").options;
		var data = $.data(target, "nrietgrid").data;
		if(opts.idField) {
			var _indx = -1;
			for(var i = 0; i < data.rows.length; i++) {
				if(data.rows[i][opts.idField] == id) {
					_indx = i;
					break;
				}
			}
			if(_indx >= 0) {
				selectRow(target, _indx);
			}
		}
	};

	function unSelectColumn(target, field) {
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		var data = $.data(target, "nrietgrid").data;
		var selectColumns = $.data(target, "nrietgrid").selectedColumns;
		var fields = getColumnFields(target, true).add(getColumnFields(target, false));
		if(fields.indexOf(field) < 0) {
			return;
		}
		var tds = $("div.nrietgrid-body td[field=" + field + "]", pnl);
		tds.removeClass("nrietgrid-col-selected");
		tds.eq(0).removeClass("nrietgrid-col-selected-top");
		tds.eq(tds.length - 1).removeClass("nrietgrid-col-selected-bottom");
		tds.each(function() {
			$(this).prev().eq(0).removeClass("nrietgrid-col-selected-prev");
		});
		var columndata = [];
		for(var i = 0; i < data.rows.length; i++) {
			columndata[i] = data.rows[i][field];
		}

		(function() {
			for(var i = 0; i < selectColumns.length; i++) {
				if(selectColumns[i].field == field) {
					selectColumns.remove(selectColumns[i]);
					break;
				}
			}
		})();

		fixColumnSize(target);
		fixColumnWidth(target);
		opts.onUnselectColumn.call(target, field, columndata);
	};

	function unSelectRow(target, index) {
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		var data = $.data(target, "nrietgrid").data;
		var selectRows = $.data(target, "nrietgrid").selectedRows;
		if(index < 0 || index >= data.rows.length) {
			return;
		}
		var pnlBody = pnl.find("div.nrietgrid-body");
		var tr = $("tr[nrietgrid-row-index=" + index + "]", pnlBody);
		var ck = $("tr[nrietgrid-row-index=" + index + "] div.nrietgrid-cell-check input[type=checkbox]", pnlBody);
		tr.removeClass("nrietgrid-row-selected");
		ck.attr("checked", false);
		
		var pnlHeader = pnl.find("div.nrietgrid-header");
		pnlHeader.find('input[type=checkbox]')._propAttr('checked', false);
		
		var row = data.rows[index];
		if(opts.idField) {
			selectRows.removeById(opts.idField, row[opts.idField]);
		}
		opts.onUnselect.call(target, index, row);
	};

	function unSelectCell(target, rowindex, tdfield) {
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		var data = $.data(target, "nrietgrid").data;
		var selectCells = $.data(target, "nrietgrid").selectedCells;
		if(rowindex < 0 || rowindex >= data.rows.length) {
			return;
		}
		var pnlBody = pnl.find("div.nrietgrid-body");
		var tr = $("tr[nrietgrid-row-index=" + rowindex + "]", pnlBody);
		var td = tr.children("td[field='" + tdfield + "']");
		td.children("div").removeClass("nrietgrid-cell-selected");
		if(selectCells) {
			for(var i = 0; i < selectCells.length; i++) {
				var field = selectCells[i]["field"];
				if(field == tdfield && selectCells[i]["value"] == data.rows[rowindex][field]) {
					selectCells.remove(i);
				}
			}
		}
		opts.onUnSelectCell.call(target, rowindex, data.rows[rowindex], tdfield);
	};

	function beginEdit(target, index, field, flag) {
		var opts = $.data(target, "nrietgrid").options;
		var tr = opts.editConfig.getTr(target, index);
		var row = opts.editConfig.getRow(target, index);
		var td = tr.find("td[field='" + field + "']");
		if(td.hasClass("nrietgrid-cell-editing")) {
			return;
		}
		if(opts.onBeforeEdit.call(target, index, row) == false) {
			return;
		}
		td.addClass("nrietgrid-cell-editing");
		beginEditCell(target, index, field, flag);
		resizeEditor(target);
		td.find("div.nrietgrid-editable").each(function() {
			var filed = $(this).parent().attr("field");
			var ed = $.data(this, "nrietgrid.editor");
			ed.actions.setValue(ed.target, row[filed]);
		});
		validateRow(target, index);
	};

	function endEdit(target, index, canceld) {
		var opts = $.data(target, "nrietgrid").options;
		var updatedRows = $.data(target, "nrietgrid").updatedRows;
		var insertedRows = $.data(target, "nrietgrid").insertedRows;
		var tr = opts.editConfig.getTr(target, index);
		var row = opts.editConfig.getRow(target, index);
		if(!tr.children("td").hasClass("nrietgrid-cell-editing")) {
			return;
		}
		if(!canceld) {
			if(!validateRow(target, index)) {
				return;
			}
			var flag = false;
			var tmpObj = {};
			tr.find("div.nrietgrid-editable").each(function() {
				var field = $(this).parent().attr("field");
				var ed = $.data(this, "nrietgrid.editor");
				var editValue = ed.actions.getValue(ed.target);
				if(row[field] != editValue.toString()) {
					row[field] = editValue;
					flag = true;
					tmpObj[field] = editValue;
				}
			});
			if(flag) {
				if(insertedRows.indexOf(row) == -1) {
					if(updatedRows.indexOf(row) == -1) {
						updatedRows.push(row);
					}
				}
			}
		}
		tr.children("td.nrietgrid-cell-editing").removeClass("nrietgrid-cell-editing");
		destroyEdit(target, index);

		$(target).each(function() {
			var opts = $.data(this, "nrietgrid").options;
			opts.view.refreshRow.call(opts.view, this, index);
		});
		if(!canceld) {
			opts.onAfterEdit.call(target, index, row, tmpObj);
		} else {
			opts.onCancelEdit.call(target, index, row);
		}
	};

	function getEditors(target, index) {
		var editors = [];
		var pnl = $.data(target, "nrietgrid").panel;
		var tr = $("div.nrietgrid-body tr[nrietgrid-row-index=" + index + "]", pnl);
		tr.children("td").each(function() {
			var editorsAry = $(this).find("div.nrietgrid-editable");
			if(editorsAry.length) {
				var ed = $.data(editorsAry[0], "nrietgrid.editor");
				editors.push(ed);
			}
		});
		return editors;
	};

	function getEditor(target, options) {
		var editors = getEditors(target, options.index);
		for(var i = 0; i < editors.length; i++) {
			if(editors[i].field == options.field) {
				return editors[i];
			}
		}
		return null;
	};

	function beginEditCell(target, index, field, flag) {
		var opts = $.data(target, "nrietgrid").options;
		var tr = opts.editConfig.getTr(target, index);
		tr.children("td[field='" + field + "']").each(function() {
			var cell = $(this).find("div.nrietgrid-cell");
			var thisFiled = $(this).attr("field");
			var col = getColOptions(target, thisFiled);
			if(col && col.editor) {
				var tmpType, tmpOpts;
				if(typeof col.editor == "string") {
					tmpType = col.editor;
				} else {
					tmpType = col.editor.type;
					tmpOpts = col.editor.options;
				}

				var regnum = /^n_/;
				var reginp = /^i_/;
				var regsel = /^s_/;
				if(regnum.test(tmpType) && !flag) {
					tmpType = "numbox";
				} else if(reginp.test(tmpType) && !flag) {
					tmpType = "text";
				} else if(regsel.test(tmpType) && !flag) {
					tmpType = "npanelbox";
				} else if(regnum.test(tmpType) && flag) {
					tmpType = "panelnumbox";
				} else if(flag) {
					return;
				}
				var tmpEditor = opts.editors[tmpType];
				if(tmpEditor) {
					var cellHtml = cell.html();
					var cellOutWidth = cell.outerWidth();
					cell.addClass("nrietgrid-editable");
					cell.attr({ "oldval": cellHtml });
					if($.boxModel == true) {
						cell.css("width", cellOutWidth - (cell.outerWidth() - cell.width()));
					}
					cell.html("<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\"><tr><td></td></tr></table>");
					cell.children("table").attr("align", col.align);
					cell.children("table").bind("click dblclick contextmenu", function(e) {
						e.stopPropagation();
					});
					$.data(cell[0], "nrietgrid.editor", { actions: tmpEditor, target: tmpEditor.init(cell.find("td"), tmpOpts), field: thisFiled, type: tmpType, oldHtml: cellHtml });
				}
			}
		});
		fixRowHeight(target, index);
	};

	function destroyEdit(target, index) {
		var opts = $.data(target, "nrietgrid").options;
		var tr = opts.editConfig.getTr(target, index);
		tr.children("td").each(function() {
			var cell = $(this).find("div.nrietgrid-editable");
			if(cell.length) {
				var ed = $.data(cell[0], "nrietgrid.editor");
				if(ed.actions.destroy) {
					ed.actions.destroy(ed.target);
				}
				cell.html(ed.oldHtml);
				$.removeData(cell[0], "nrietgrid.editor");
				var cellOuterWidth = cell.outerWidth();
				cell.removeClass("nrietgrid-editable");
				cell.removeClass("nrietgrid-cell-selected");
				if($.boxModel == true) {
					cell.css("width", cellOuterWidth - (cell.outerWidth() - cell.width()));
				}
			}
		});
	};

	function validateRow(target, index) {
		var tr = $.data(target, "nrietgrid").options.editConfig.getTr(target, index);
		if(!tr.children("td").hasClass("nrietgrid-cell-editing")) {
			return true;
		}
		var vbox = tr.find(".validatebox-text");
		vbox.validatebox("validate");
		vbox.trigger("mouseleave");
		var trAry = tr.find(".validatebox-invalid");
		return trAry.length == 0;
	};

	function bindRowEvents(target) {
		var pnl = $.data(target, "nrietgrid").panel;
		var opts = $.data(target, "nrietgrid").options;
		var data = $.data(target, "nrietgrid").data;
		var pnlBody = pnl.find("div.nrietgrid-body");
		pnlBody.find("tr[nrietgrid-row-index]").unbind(".nrietgrid").bind("mouseenter.nrietgrid", function() {
			var findIndex = $(this).attr("nrietgrid-row-index");
			pnlBody.find("tr[nrietgrid-row-index=" + findIndex + "]").addClass("nrietgrid-row-over");
		}).bind("mouseleave.nrietgrid", function() {
			var findIndex = $(this).attr("nrietgrid-row-index");
			pnlBody.find("tr[nrietgrid-row-index=" + findIndex + "]").removeClass("nrietgrid-row-over");
		}).bind("click.nrietgrid", function(e) {
			var findtd = $(e.target).closest("td");
			var findIndex = $(this).attr("nrietgrid-row-index");
			var tdfield = findtd.attr("field");
			var tdindex = getTdIndex(findIndex, findtd);
			if(opts.cellSelect == true && tdindex > 0) {
				if(opts.singleSelect == true) {
					clearSelections(target);
					selectCell(target, findIndex, tdfield);
				} else {
					if(findtd.children("div").hasClass("nrietgrid-cell-selected")) {
						unSelectCell(target, findIndex, tdfield);
					} else {
						selectCell(target, findIndex, tdfield);
					}
				}
			} else {
				if(opts.singleSelect == true) {
					clearSelections(target);
					selectRow(target, findIndex);
				} else {
					if($(this).hasClass("nrietgrid-row-selected")) {
						unSelectRow(target, findIndex);
					} else {
						selectRow(target, findIndex);
					}
				}
			}

			if(opts.onClickRow) {
				opts.onClickRow.call(target, findIndex, data.rows[findIndex]);
			}
		}).bind("dblclick.nrietgrid", function() {
			var findIndex = $(this).attr("nrietgrid-row-index");
			if(opts.onDblClickRow) {
				opts.onDblClickRow.call(target, findIndex, data.rows[findIndex]);
			}
		}).bind("contextmenu.nrietgrid", function(e) {
			var findIndex = $(this).attr("nrietgrid-row-index");
			if(opts.onRowContextMenu) {
				opts.onRowContextMenu.call(target, e, findIndex, data.rows[findIndex]);
			}

		});
		pnlBody.find("td[field]").unbind(".nrietgrid").bind("click.nrietgrid", function() {
			var findIndex = $(this).parent().attr("nrietgrid-row-index");
			var findField = $(this).attr("field");
			var findRow = data.rows[findIndex][findField];
			var col = getColOptions(target, findField);
			clearEdit(target);
			if(opts.editIndex != findIndex && col.editor) {
				clearEdit(target);
				beginEdit(target, findIndex, findField);
				var edt = getEditor(target, { "index": findIndex, "field": findField });
				edt.target.focus();
				$($(this).find("input:visible")[0]).focus();
				opts.editIndex = findIndex;
			}

			opts.onClickCell.call(target, findIndex, findField, findRow);
		}).bind("dblclick.nrietgrid", function() {
			var findIndex = $(this).parent().attr("nrietgrid-row-index");
			var findField = $(this).attr("field");
			var findRow = data.rows[findIndex][findField];
			opts.onDblClickCell.call(target, findIndex, findField, findRow);
		}).bind("contextmenu.nrietgrid", function(e) {
			var findIndex = $(this).parent().attr("nrietgrid-row-index");
			var findField = $(this).attr("field");
			var findRow = data.rows[findIndex][findField];
			var col = getColOptions(target, findField);
			clearEdit(target);
			if(opts.editIndex != findIndex && col.editor && (col.title.indexOf("温") > -1 || col.title.indexOf("水") > -1)) {
				clearEdit(target);
				beginEdit(target, findIndex, findField, true);
				var edt = getEditor(target, { "index": findIndex, "field": findField });
				edt.target.focus();
				opts.editIndex = findIndex;
			}
			if(opts.onRowContextMenu) {
				opts.onRowContextMenu.call(target, e, findIndex, data.rows[findIndex]);
			}
			e.preventDefault();
		}).bind('mouseenter.nrietgrid', function(e){
			var findIndex = $(this).parent().attr("nrietgrid-row-index");
			var findField = $(this).attr("field");
			var findRow = data.rows[findIndex][findField];
			var col = getColOptions(target, findField);
			if (col.copy) {
				var copy = col.copy;
				if (copy.copyRow.indexOf(parseInt(findIndex)) != -1) {
					$("div.nrietgrid-copy").parent().remove();
					var height = $(this).height();
					var width = $(this).width();
					var pnl = $("<div style='position:absolute'><div class='nrietgrid-copy'></div></div>").appendTo($(this));
					pnl.find(".nrietgrid-copy").css({
						"top": -height+15 + "px",
						"left": width-25 + "px"
					});
					
					var pageleft = getLeft(pnl[0]);
					var pagetop = getTop(pnl[0]);
					var scrollTop = 0;
					scrollTop = $(".tf-grid").scrollTop();
					var outScroll = $("#iSouth").scrollTop();
					if(scrollTop == 0 && outScroll > 0){
						scrollTop = outScroll;
					}
					if(!scrollTop){
						scrollTop = 0;
					}
					
					pnl.appendTo($(this).closest('div.nrietpanel.nrietgrid')).css({
						"top": pagetop - scrollTop,
						"left": pageleft + 8,
						"z-index": 99999
					}).bind('click.nrietgrid', function(){
						clearEdit(target);
						var copyRow = data.rows[findIndex];
						var pasteRowIndex = copy.pasteRow[parseInt(findIndex)];
						if (typeof pasteRowIndex == 'number') {
							pasteRowIndex = copy.pasteRow;
						}
						var copyField  = copy.copyField;
						var pasteRow; 
						for (var i=0; i < pasteRowIndex.length; i++) {
							pasteRow = data.rows[pasteRowIndex[i]];
							for (var j=0; j < copyField.length; j++) {
								pasteRow[copyField[j]] = copyRow[copyField[j]];
							}
							opts.view.updateRow.call(opts.view, target, pasteRowIndex[i], pasteRow);;
						}
					});
				}
			}
			e.stopPropagation();
		}).bind('mouseleave.nrietgrid', function(e){
			if (e.relatedTarget && e.relatedTarget.getAttribute('class') != 'nrietgrid-copy') {
				$("div.nrietgrid-copy").parent().remove();
			}
		});
		pnlBody.find("div.nrietgrid-cell-check input[type=checkbox]").unbind(".nrietgrid").bind("click.nrietgrid", function(e) {
			var findIndex = $(this).parent().parent().parent().attr("nrietgrid-row-index");
			if(opts.singleSelect) {
				clearSelections(target);
				selectRow(target, findIndex);
			} else {
				if($(this).prop("checked")) {
					selectRow(target, findIndex);
				} else {
					unSelectRow(target, findIndex);
				}
			}
			if($(this).prop("checked")) {
				$(this).parents("tr").addClass("nrietgrid-row-selected");
			} else {
				$(this).parents("tr").removeClass("nrietgrid-row-selected");
			}
			e.stopPropagation();
		});

		function getTdIndex(rowindex, td) {
			var tds = pnlBody.find("tr[nrietgrid-row-index='" + rowindex + "']").children("td:not(:hidden):not(.nrietgrid-td-rownumber)");
			//        	console.log(tds.index(td));
			return tds.index(td);
		}
	};

	function clearEdit(target) {
		target = $($(".nrietgrid-resize-proxy").next("div"));// 修改为当一个页面中有几个表格时，点击当前表格内容时其他的表格编辑或选中状态不消失的问题
		if(target.length > 0){
			$.each(target, function(){
				var t = $(this);
				if(!t.length) {
					return;
				}
				var opts = $.data(this, "nrietgrid").options;
				var editIndex = opts.editIndex;
				if(editIndex == undefined) {
					return;
				}
				var ed = getEditors(this, editIndex)[0];
				if(ed) {
					ed.target.blur();
					if(validateRow(this, editIndex)) {
						endEdit(this, editIndex);
					} else {
						cancelEdit(this, editIndex);
					}
				}
				opts.editIndex = undefined;
			});
		}
	};

	function bindCellsEvents(target) {
		var pnl = $.data(target, "nrietgrid").panel;
		var opts = $.data(target, "nrietgrid").options;
		var pnlHeader = pnl.find("div.nrietgrid-header");
		pnlHeader.find("td:has(div.nrietgrid-cell)").unbind(".nrietgrid").bind("mouseenter.nrietgrid", function() {
			if($.data(target, "nrietgrid").resizing) {
				return;
			}
			$(this).addClass("nrietgrid-header-over");
		}).bind("mouseleave.nrietgrid", function() {
			$(this).removeClass("nrietgrid-header-over");
		}).bind("contextmenu.nrietgrid", function(e) {
			var filed = $(this).attr("field");
			opts.onHeaderContextMenu.call(target, e, filed);
		});
		pnlHeader.find("div.nrietgrid-cell").unbind(".nrietgrid").bind("click.nrietgrid", function() {
			if(!opts.columnSelect) {
				return;
			}

			var field = $(this).parent().attr("field");
			if(opts.singleSelect) {
				clearSelections(target);
				selectColumn(target, field);
			} else {
				if($(this).parent().hasClass("nrietgrid-col-selected")) {
					unSelectColumn(target, field);
				} else {
					selectColumn(target, field);
				}
			}
		});
		pnlHeader.find("div.nrietgrid-cell-group").unbind(".nrietgrid").bind("click.nrietgrid", function() {
			if(!opts.columnSelect) {
				return;
			}
			var pos = getPosition($(this).parent()[0]);
			//        	console.log(pos);
			var fields = containsfield($(this).parent()[0], pos);
			clearEdit(target);// 清除编辑框
			if(opts.singleSelect) {
				clearSelections(target);
				for(var f = 0; f < fields.length; f++) {
					field = fields[f];
					selectColumn(target, field);
				}
			} else {
				for(var f = 0; f < fields.length; f++) {
					field = fields[f];
					if($(this).parent().hasClass("nrietgrid-col-selected")) {
						unSelectColumn(target, field);
					} else {
						selectColumn(target, field);
					}
				}
			}
		});

		pnlHeader.find("div span.nrietgrid-sort-icon").unbind(".nrietgrid").bind("click.nrietgrid", function() {
			var filed = $(this).parent().hasClass("nrietgrid-cell-group") ? $(this).closest("td").attr("sortfield") : $(this).closest("td").attr("field");
			var opt = getColOptions(target, filed);
			if(!opt.sortable || $.data(target, "nrietgrid").resizing) {
				return;
			}

			opts.sortName = filed;
			opts.sortOrder = "asc";
			var c = "nrietgrid-sort-asc";
			if($(this).parent().hasClass("nrietgrid-sort-asc")) {
				c = "nrietgrid-sort-desc";
				opts.sortOrder = "desc";
			}
			pnlHeader.find("div.nrietgrid-cell").removeClass("nrietgrid-sort-asc nrietgrid-sort-desc");
			$(this).parent().addClass(c);
			if(opts.onSortColumn) {
				opts.onSortColumn.call(target, opts.sortName, opts.sortOrder);
			}
			if(opts.remoteSort) {
				request(target);
			} else {
				var data = $.data(target, "nrietgrid").data;
				loadData(target, data);
			}
		});
		pnlHeader.find("input[type=checkbox]").unbind(".nrietgrid").bind("click.nrietgrid", function() {
			if(opts.singleSelect && opts.selectOnCheck) {
				return false;
			}
			if($(this).prop("checked")) {
				selectAll(target);
			} else {
				clearSelectRows(target);
			}
		});

		var view = pnl.children("div.nrietgrid-view");
		var view1 = view.children("div.nrietgrid-view1");
		var view2 = view.children("div.nrietgrid-view2");
		view2.children("div.nrietgrid-body").unbind(".nrietgrid").bind("scroll.nrietgrid", function() {
			view1.children("div.nrietgrid-body").scrollTop($(this).scrollTop());
			view2.children("div.nrietgrid-header").scrollLeft($(this).scrollLeft());
			view2.children("div.nrietgrid-footer").scrollLeft($(this).scrollLeft());
		});
		view1.children("div.nrietgrid-body").unbind(".nrietgrid").bind("scroll.nrietgrid", function() {
			view2.children("div.nrietgrid-body").scrollTop($(this).scrollTop());
			view1.children("div.nrietgrid-header").scrollLeft($(this).scrollLeft());
			view1.children("div.nrietgrid-footer").scrollLeft($(this).scrollLeft());
		});
		pnlHeader.find("div.nrietgrid-cell").each(function() {
			$(this).resizable({
				handles: "e",
				disabled: ($(this).attr("resizable") ? $(this).attr("resizable") == "false" : false),
				minWidth: 25,
				onStartResize: function(e) {
					$.data(target, "nrietgrid").resizing = true;
					pnlHeader.css("cursor", $("body").css("cursor"));
					view.children("div.nrietgrid-resize-proxy").css({ left: e.pageX - $(pnl).offset().left - 1, display: "block" });
				},
				onResize: function(e) {
					view.children("div.nrietgrid-resize-proxy").css({ display: "block", left: e.pageX - $(pnl).offset().left - 1 });
					return false;
				},
				onStopResize: function(e) {
					var field = $(this).parent().attr("field");
					var col = getColOptions(target, field);
					col.width = $(this).outerWidth();
					col.boxWidth = $.boxModel == true ? $(this).width() : $(this).outerWidth();
					fixColumnSize(target, field);
					fixColumnWidth(target);
					var tmpView = pnl.find("div.nrietgrid-view2");
					tmpView.find("div.nrietgrid-header").scrollLeft(tmpView.find("div.nrietgrid-body").scrollLeft());
					view.children("div.nrietgrid-resize-proxy").css("display", "none");
					opts.onResizeColumn.call(target, field, col.width);
				}
			});
		});
		view1.children("div.nrietgrid-header").find("div.nrietgrid-cell").resizable({
			onStopResize: function(e) {
				var filed = $(this).parent().attr("field");
				var col = getColOptions(target, filed);
				col.width = $(this).outerWidth();
				col.boxWidth = $.boxModel == true ? $(this).width() : $(this).outerWidth();
				fixColumnSize(target, filed);
				var tmpView = pnl.find("div.nrietgrid-view2");
				tmpView.find("div.nrietgrid-header").scrollLeft(tmpView.find("div.nrietgrid-body").scrollLeft());
				view.children("div.nrietgrid-resize-proxy").css("display", "none");
				fixDataGridHeight(target);
				fixColumnWidth(target);
				opts.onResizeColumn.call(target, filed, col.width);
			}
		});
	};

	function containsfield(td, pos) {
		var $table = $(td).closest("table");
		var tfields = $table.find("td[field]");
		var fields = [];
		var colspan = $(td).attr("colspan") || 1;

		for(var i = 0; i < colspan; i++) {
			var field = tfields.eq(pos.col - 1 + i).attr("field");
			fields.push(field);
		}

		return fields
	}

	function highlightrow(target, index) {
		var $grid = $.data(target, "nrietgrid");
		var opts = $grid.options;
		opts.finder.getTr(target, $grid.highlightIndex).removeClass("nrietgrid-row-over");
		opts.finder.getTr(target, index).addClass("nrietgrid-row-over");
		$grid.highlightIndex = index;
	};

	function loadData(target, data) {
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		var selectRows = $.data(target, "nrietgrid").selectedRows;
		data = opts.loadFilter.call(target, data);
		var dataRows = data.rows;
		$.data(target, "nrietgrid").data = data;
		if(data.footer) {
			$.data(target, "nrietgrid").footer = data.footer;
		}
		if(!opts.remoteSort) {
			var opt = getColOptions(target, opts.sortName);
			if(opt) {
				var _sorter = opt.sorter || function(a, b) {
					return(a > b ? 1 : -1);
				};
				data.rows.sort(function(r1, r2) {
					return _sorter(r1[opts.sortName], r2[opts.sortName]) * (opts.sortOrder == "asc" ? 1 : -1);
				});
			}
		}
		var view = pnl.children("div.nrietgrid-view");
		var view1 = view.children("div.nrietgrid-view1");
		var view2 = view.children("div.nrietgrid-view2");
		if(opts.view.onBeforeRender) {
			opts.view.onBeforeRender.call(opts.view, target, dataRows);
		}
		opts.view.render.call(opts.view, target, view2.children("div.nrietgrid-body"), false);
		opts.view.render.call(opts.view, target, view1.children("div.nrietgrid-body").children("div.nrietgrid-body-inner"), true);
		if(opts.showFooter) {
			opts.view.renderFooter.call(opts.view, target, view2.find("div.nrietgrid-footer-inner"), false);
			opts.view.renderFooter.call(opts.view, target, view1.find("div.nrietgrid-footer-inner"), true);
		}
		if(opts.view.onAfterRender) {
			opts.view.onAfterRender.call(opts.view, target);
		}

		if(opts.fitColumns) {
			fixColumnWidth(target);
		}

		opts.onLoadSuccess.call(target, data);
		//分页组件pagination未开发
		//      var pager = pnl.children("div.nrietgrid-pager");  
		//      if (pager.length) {  
		//          if (pager.pagination("options").total != data.total) {  
		//              pager.pagination({ total: data.total });  
		//          }  
		//      }  
		fixRowHeight(target);
		//    	headFilterColumn(target);
		bindRowEvents(target);

		view2.children("div.nrietgrid-body").triggerHandler("scroll");
		if(opts.idField) {
			for(var i = 0; i < dataRows.length; i++) {
				if(isExistsRow(dataRows[i])) {
					selectRecord(target, dataRows[i][opts.idField]);
				}
			}
		}

		function isExistsRow(row) {
			for(var i = 0; i < selectRows.length; i++) {
				if(selectRows[i][opts.idField] == row[opts.idField]) {
					selectRows[i] = row;
					return true;
				}
			}
			return false;
		};
	};

	function headFilterColumn(target) {
		var opts = $.data(target, "nrietgrid").options;
		var pnl = $.data(target, "nrietgrid").panel;
		var htrs1 = pnl.find("div.nrietgrid-view1 tr.nrietgrid-header-row");
		var htrs2 = pnl.find("div.nrietgrid-view2 tr.nrietgrid-header-row");
		for(var i = 0; i < htrs2.length; i++) {
			var tr2 = htrs2.eq(i);
			var tr1 = htrs1.eq(i);

			if(!isNaN(opts.headFilter) && i == opts.headFilter) {
				tr2.css({ display: "none" });
				tr1.css({ display: "none" });
				var divheight = pnl.find("div.nrietgrid-header").height();
				var tableheight = pnl.find("div.nrietgrid-header table").height();
				var sub = tr2.height();
				pnl.find("div.nrietgrid-header").css({ "height": divheight - sub });
				pnl.find("div.nrietgrid-header table").css({ "height": tableheight - sub });
			}
		}

	}

	function getChanges(target, type) {
		var insertedRows = $.data(target, "nrietgrid").insertedRows;
		var deletedRows = $.data(target, "nrietgrid").deletedRows;
		var updatedRows = $.data(target, "nrietgrid").updatedRows;
		if(!type) {
			var rows = [];
			rows = rows.concat(insertedRows);
			rows = rows.concat(deletedRows);
			rows = rows.concat(updatedRows);
			return rows;
		} else {
			if(type == "inserted") {
				return insertedRows;
			} else {
				if(type == "deleted") {
					return deletedRows;
				} else {
					if(type == "updated") {
						return updatedRows;
					}
				}
			}
		}
		return [];
	};

	function deleteRow(taget, index) {
		var opts = $.data(taget, "nrietgrid").options;
		var data = $.data(taget, "nrietgrid").data;
		var insertedRows = $.data(taget, "nrietgrid").insertedRows;
		var deletedRows = $.data(taget, "nrietgrid").deletedRows;
		var selectedRows = $.data(taget, "nrietgrid").selectedRows;
		//        $(taget).datagrid("cancelEdit", index);  //待开发
		var row = data.rows[index];
		if(insertedRows.indexOf(row) >= 0) {
			insertedRows.remove(row);
		} else {
			deletedRows.push(row);
		}
		selectedRows.removeById(opts.idField, data.rows[index][opts.idField]);
		opts.view.deleteRow.call(opts.view, taget, index);
		if(opts.height == "auto") {
			fixRowHeight(taget);
		}
	};

	function insertRow(target, param) {
		var view = $.data(target, "nrietgrid").options.view;
		var insertedRows = $.data(target, "nrietgrid").insertedRows;
		view.insertRow.call(view, target, param.index, param.row);
		bindRowEvents(target);
		insertedRows.push(param.row);
	};

	function appendRow(target, row) {
		var view = $.data(target, "nrietgrid").options.view;
		var insertedRows = $.data(target, "nrietgrid").insertedRows;
		view.insertRow.call(view, target, null, row);
		bindRowEvents(target);
		insertedRows.push(row);
	};

	function cacheRows(target) {
		var data = $.data(target, "nrietgrid").data;
		var rows = data.rows;
		var rowAry = [];
		for(var i = 0; i < rows.length; i++) {
			rowAry.push($.extend({}, rows[i]));
		}
		$.data(target, "nrietgrid").originalRows = rowAry;
		$.data(target, "nrietgrid").updatedRows = [];
		$.data(target, "nrietgrid").insertedRows = [];
		$.data(target, "nrietgrid").deletedRows = [];
	};

	function mergeCells(target, options) {
		var rows = $.data(target, "nrietgrid").data.rows;
		var pnl = $.data(target, "nrietgrid").panel;
		options.rowspan = options.rowspan || 1;
		options.colspan = options.colspan || 1;
		if(options.index < 0 || options.index >= rows.length) {
			return;
		}
		if(options.rowspan == 1 && options.colspan == 1) {
			return;
		}
		var findRow = rows[options.index][options.field];
		var tr = pnl.find("div.nrietgrid-body tr[nrietgrid-row-index=" + options.index + "]");
		var td = tr.find("td[field=" + options.field + "]");
		td.attr("rowspan", options.rowspan).attr("colspan", options.colspan);
		td.addClass("nrietgrid-td-merged");
		for(var i = 1; i < options.colspan; i++) {
			td = td.next();
			td.hide();
			rows[options.index][td.attr("field")] = findRow;
		}
		for(var i = 1; i < options.rowspan; i++) {
			tr = tr.next();
			var td = tr.find("td[field=" + options.field + "]").hide();
			rows[options.index + i][td.attr("field")] = findRow;
			for(var j = 1; j < options.colspan; j++) {
				td = td.next();
				td.hide();
				rows[options.index + i][td.attr("field")] = findRow;
			}
		}
		setTimeout(function() {
			fixCellWidth(target);
		}, 0);
	};

	var NrietGrid = function(options) {
		options = options || {};
		$(options.gridRender).each(function() {
			var state = $.data(this, "nrietgrid");
			var opts;
			if(state) {
				opts = $.extend(state.options, options);
				state.options = opts;
			} else {
				opts = $.extend({}, panelDefaults, NrietGrid.defaults, options);
				$(this).css("width", "").css("height", "");
				var wrapResult = wrapGrid(this, opts.rownumbers);
				if(!opts.columns) {
					opts.columns = wrapResult.columns;
				}
				if(!opts.frozenColumns) {
					opts.frozenColumns = wrapResult.frozenColumns;
				}
				opts.columns = $.extend(true, [], opts.columns);
				opts.frozenColumns = $.extend(true, [], opts.frozenColumns);
				opts.view = $.extend({}, opts.view);
				$.data(this, "nrietgrid", { options: opts, panel: wrapResult.panel, dc: wrapResult.dc, ss: wrapResult.ss, selectedRows: [], checkedRows: [], selectedCells: [], selectedColumns: [], data: { total: 0, rows: [] }, originalRows: [], updatedRows: [], insertedRows: [], deletedRows: [] });
			}
			initDatagrid(this);
			if(opts.data) {
				loadData(this, opts.data);
				cacheRows(this);
			} else {
				var data = getData(this);
				if(data.total > 0) {
					loadData(this, data);
					cacheRows(this);
				}
			}
			setSize(this);
			if(opts.url) {
				request(this);
			}
			bindCellsEvents(this);
		});
	};

	var panelDefaults = {
		id: null, //面板的ID属性
		title: null, //在面板头部显示的标题文本
		iconCls: null, //设置一个16x16图标的CSS类ID显示在面板左上角
		width: "auto", //设置面板宽度
		height: "auto", //设置面板高度
		left: null, //设置面板距离左边的位置（即X轴位置）
		top: null, //设置面板距离顶部的位置（即Y轴位置）
		cls: null, //添加一个CSS类ID到面板
		headerCls: null, //添加一个CSS类ID到面板头部
		bodyCls: null, //添加一个CSS类ID到面板正文部分
		style: {}, //添加一个当前指定样式到面板
		href: null, //从URL读取远程数据并且显示到面板。注意：内容将不会被载入，直到面板打开或扩大，在创建延迟加载面板时是非常有用的：
		cache: true, //如果为true，在超链接载入时缓存面板内容
		fit: false, //当设置为true的时候面板大小将自适应父容器。下面的例子显示了一个面板，可以自动在父容器的最大范围内调整大小
		border: true, //定义是否显示面板边框
		doSize: true, //如果设置为true，在面板被创建的时候将重置大小和重新布局
		noheader: false, //如果设置为true，那么将不会创建面板标题
		content: null, //面板主体内容
		collapsible: false, //定义是否显示可折叠按钮
		minimizable: false, //定义是否显示最小化按钮
		maximizable: false, //定义是否显示最大化按钮
		closable: false, //定义是否显示关闭按钮
		collapsed: false, //定义是否在初始化的时候折叠面板
		minimized: false, //定义是否在初始化的时候最小化面板
		maximized: false, //定义是否在初始化的时候最大化面板
		closed: false, //定义是否在初始化的时候关闭面板
		tools: null, //自定义工具菜单，可用值： 1) 数组，每个元素都包含'iconCls'和'handler'属性。2) 指向工具菜单的选择器。面板工具菜单可以声明在已经存在的<div>标签上
		loadingMessage: "Loading...", //在加载远程数据的时候在面板内显示一条消息
		extractor: function(data) { //定义如何从ajax应答数据中提取内容，返回提取数据
			var pattern = /<body[^>]*>((.|[\n\r])*)<\/body>/im;
			var matches = pattern.exec(data);
			if(matches) {
				return matches[1];
			} else {
				return data;
			}
		},
		//在加载远程数据时触发
		onLoad: function() {},
		//在打开面板之前触发，返回false可以取消打开操作
		onBeforeOpen: function() {},
		//在打开面板之后触发
		onOpen: function() {},
		//在关闭面板之前触发，返回false可以取消关闭操作。下列的面板将不能关闭
		onBeforeClose: function() {},
		//在面板关闭之后触发
		onClose: function() {},
		//在面板销毁之前触发，返回false可以取消销毁操作
		onBeforeDestroy: function() {},
		//在面板销毁之后触发
		onDestroy: function() {},
		//在面板改变大小之后触发 width：新的宽度。        height：新的高度
		onResize: function(width, height) {},
		//在面板移动之后触发。   left：新的左边距位置。     top：新的上边距位置
		onMove: function(left, top) {},
		//在窗口最大化之后触发
		onMaximize: function() {},
		//在窗口恢复到原始大小以后触发
		onRestore: function() {},
		//在窗口最小化之后触发
		onMinimize: function() {},
		//在面板折叠之前触发，返回false可以终止折叠操作
		onBeforeCollapse: function() {},
		//在面板展开之前触发，返回false可以终止展开操作
		onBeforeExpand: function() {},
		//在面板折叠之后触发
		onCollapse: function() {},
		//在面板展开之后触发
		onExpand: function() {}
	};

	function loading(target) {
		var opts = $.data(target, "nrietgrid").options;
		//分页组件pagination未开发
		//          $(this).datagrid("getPager").pagination("loading");  
		if(opts.loadMsg) {
			var wrap = $.data(target, "nrietgrid").panel;
			$("<div class=\"nrietgrid-mask\"></div>").css({ display: "block", width: wrap.width(), height: wrap.height() }).appendTo(wrap);
			$("<div class=\"nrietgrid-mask-msg\"></div>").html(opts.loadMsg).appendTo(wrap).css({ display: "block", left: (wrap.width() - $("div.nrietgrid-mask-msg", wrap).outerWidth()) / 2, top: (wrap.height() - $("div.nrietgrid-mask-msg", wrap).outerHeight()) / 2 });
		}
	}

	function loaded(target) {
		//分页组件pagination未开发			
		//          $(this).datagrid("getPager").pagination("loaded");  
		var pnl = $.data(target, "nrietgrid").panel;
		pnl.children("div.nrietgrid-mask-msg").remove();
		pnl.children("div.nrietgrid-mask").remove();
	}

	NrietGrid.prototype = {
		options: function() {
			var jq = this.target;
			var opts = $.data(jq[0], "nrietgrid").options;
			var pnlOpts = $.data($.data(jq[0], "nrietgrid").panel.get(0), "nrietpanel").options;
			var opts = $.extend(opts, { width: pnlOpts.width, height: pnlOpts.height, closed: pnlOpts.closed, collapsed: pnlOpts.collapsed, minimized: pnlOpts.minimized, maximized: pnlOpts.maximized });
			//分页组件pagination未开发
			//      var pager = jq.datagrid("getPager");  
			//      if (pager.length) {  
			//          var pagerOpts = pager.pagination("options");  
			//          $.extend(opts, { pageNumber: pagerOpts.pageNumber, pageSize: pagerOpts.pageSize });  
			//      }  
			return opts;
		},
		getPanel: function() {
			var jq = this.target;
			return $.data(jq[0], "nrietgrid").panel;
		},
		getPager: function() {
			var jq = this.target;
			return $.data(jq[0], "nrietgrid").panel.find("div.nrietgrid-pager");
		},
		getColumnFields: function(frozen) {
			var jq = this.target;
			return getColumnFields(jq[0], frozen);
		},
		getColumnOption: function(filed) {
			var jq = this.target;
			return getColOptions(jq[0], filed);
		},
		resize: function(params) {
			var jq = this.target;
			return jq.each(function() {
				setSize(this, params);
			});
		},
		load: function(param) {
			var jq = this.target;
			return jq.each(function() {
				var opts = $.data(this, "nrietgrid").options;
				opts.pageNumber = 1;
				//分页组件pagination未开发
				//          var pager = $(this).datagrid("getPager");  
				//          pager.pagination({ pageNumber: 1 });  
				request(this, param);
			});
		},
		reload: function(param) {
			var jq = this.target;
			return jq.each(function() {
				request(this, param);
			});
		},
		reloadFooter: function(footer) {
			var jq = this.target;
			return jq.each(function() {
				var opts = $.data(this, "nrietgrid").options;
				var view = $.data(this, "nrietgrid").panel.children("div.nrietgrid-view");
				var view1 = view.children("div.nrietgrid-view1");
				var view2 = view.children("div.nrietgrid-view2");
				if(footer) {
					$.data(this, "nrietgrid").footer = footer;
				}
				if(opts.showFooter) {
					opts.view.renderFooter.call(opts.view, this, view2.find("div.nrietgrid-footer-inner"), false);
					opts.view.renderFooter.call(opts.view, this, view1.find("div.nrietgrid-footer-inner"), true);
					if(opts.view.onAfterRender) {
						opts.view.onAfterRender.call(opts.view, this);
					}
					fixRowHeight(this);
				}
			});
		},
		loading: function() {
			var jq = this.target;
			return jq.each(function() {
				loading(this);
			});
		},
		loaded: function() {
			var jq = this.target;
			return jq.each(function() {
				loaded(this);
			});
		},
		fitColumns: function() {
			var jq = this.target;
			return jq.each(function() {
				fixColumnWidth(this);
				//	        	headFilterColumn(this);
			});
		},
		fixColumnSize: function() {
			var jq = this.target;
			return jq.each(function() {
				fixColumnSize(this);
			});
		},
		fixRowHeight: function(index) {
			var jq = this.target;
			return jq.each(function() {
				fixRowHeight(this, index);
			});
		},
		loadData: function(data) {
			var jq = this.target;
			return jq.each(function() {
				loadData(this, data);
				cacheRows(this);
			});
		},
		getData: function() {
			var jq = this.target;
			return $.data(jq[0], "nrietgrid").data;
		},
		getRows: function() {
			var jq = this.target;
			return $.data(jq[0], "nrietgrid").data.rows;
		},
		getFooterRows: function() {
			var jq = this.target;
			return $.data(jq[0], "nrietgrid").footer;
		},
		getRowIndex: function(id) {
			var jq = this.target;
			return getRowIndex(jq[0], id);
		},
		getRowByIndex: function(index) {
			var jq = this.target;
			return $.data(jq[0], "nrietgrid").data.rows[index];
		},
		getSelected: function() {
			var jq = this.target;
			var rows = getSelectRows(jq[0]);
			return rows.length > 0 ? rows[0] : null;
		},
		getSelections: function() {
			var jq = this.target;
			return getSelectRows(jq[0]);
		},
		getSelectedCells: function() {
			var jq = this.target;
			return $.data(jq[0], "nrietgrid").selectedCells;
		},
		getSelectedColumns: function() {
			var jq = this.target;
			return $.data(jq[0], "nrietgrid").selectedColumns;
		},
		clearSelections: function() {
			var jq = this.target;
			return jq.each(function() {
				clearSelections(this);
			});
		},
		selectAll: function() {
			var jq = this.target;
			return jq.each(function() {
				selectAll(this);
			});
		},
		unselectAll: function() {
			var jq = this.target;
			return jq.each(function() {
				clearSelectRows(this);
			});
		},
		selectRow: function(index) {
			var jq = this.target;
			return jq.each(function() {
				selectRow(this, index);
			});
		},
		selectRecord: function(id) {
			var jq = this.target;
			return jq.each(function() {
				selectRecord(this, id);
			});
		},
		unselectRow: function(index) {
			var jq = this.target;
			return jq.each(function() {
				unSelectRow(this, index);
			});
		},
		beginEdit: function(index, field) {
			var jq = this.target;
			return jq.each(function() {
				beginEdit(this, index, field);
			});
		},
		endEdit: function(index) {
			var jq = this.target;
			return jq.each(function() {
				endEdit(this, index, false);
			});
		},
		cancelEdit: function(index) {
			var jq = this.target;
			return jq.each(function() {
				endEdit(this, index, true);
			});
		},
		getEditors: function(index) {
			var jq = this.target;
			return getEditors(jq[0], index);
		},
		getEditor: function(options) {
			var jq = this.target;
			return getEditor(jq[0], options);
		},
		refreshRow: function(index) {
			var jq = this.target;
			return jq.each(function() {
				var opts = $.data(this, "nrietgrid").options;
				opts.view.refreshRow.call(opts.view, this, index);
			});
		},
		//		validateRow: function (jq, index) {  
		//			return validateRow(jq[0], index);  
		//		},
		updateRow: function(param) {
			var jq = this.target;
			return jq.each(function() {
				var opts = $.data(this, "nrietgrid").options;
				opts.view.updateRow.call(opts.view, this, param.index, param.row);
			});
		},
		appendRow: function(row) {
			var jq = this.target;
			return jq.each(function() {
				appendRow(this, row);
			});
		},
		insertRow: function(param) {
			var jq = this.target;
			return jq.each(function() {
				insertRow(this, param);
			});
		},
		deleteRow: function(index) {
			var jq = this.target;
			return jq.each(function() {
				deleteRow(this, index);
			});
		},
		getChanges: function(type) {
			var jq = this.target;
			return getChanges(jq[0], type);
		},
		//		acceptChanges: function (jq) {  
		//			return jq.each(function () {  
		//				acceptChanges(this);  
		//			});  
		//		}, rejectChanges: function (jq) {  
		//			return jq.each(function () {  
		//				rejectChanges(this);  
		//			});  
		//		},
		mergeCells: function(options) {
			var jq = this.target;
			return jq.each(function() {
				mergeCells(this, options);
			});
		},
		showColumn: function(filed) {
			var jq = this.target;
			return jq.each(function() {
				var pnl = $.data(this, "nrietgrid").panel;
				pnl.find("td[field=" + filed + "]").show();
				getColOptions(this, filed).hidden = false;
				fixColumnWidth(this);
				//	        	headFilterColumn(this);  
			});
		},
		hideColumn: function(filed) {
			var jq = this.target;
			return jq.each(function() {
				var pnl = $.data(this, "nrietgrid").panel;
				pnl.find("td[field=" + filed + "]").hide();
				getColOptions(this, filed).hidden = true;
				fixColumnWidth(this);
				//	        	headFilterColumn(this);
			});
		}
	};

	NrietGrid.parseOptions = function(target) {
		var t = $(target);
		return $.extend({}, { fitColumns: (t.attr("fitColumns") ? t.attr("fitColumns") == "true" : undefined), striped: (t.attr("striped") ? t.attr("striped") == "true" : undefined), nowrap: (t.attr("nowrap") ? t.attr("nowrap") == "true" : undefined), rownumbers: (t.attr("rownumbers") ? t.attr("rownumbers") == "true" : undefined), singleSelect: (t.attr("singleSelect") ? t.attr("singleSelect") == "true" : undefined), pagination: (t.attr("pagination") ? t.attr("pagination") == "true" : undefined), pageSize: (t.attr("pageSize") ? parseInt(t.attr("pageSize")) : undefined), pageList: (t.attr("pageList") ? eval(t.attr("pageList")) : undefined), remoteSort: (t.attr("remoteSort") ? t.attr("remoteSort") == "true" : undefined), showHeader: (t.attr("showHeader") ? t.attr("showHeader") == "true" : undefined), showFooter: (t.attr("showFooter") ? t.attr("showFooter") == "true" : undefined), scrollbarSize: (t.attr("scrollbarSize") ? parseInt(t.attr("scrollbarSize")) : undefined), loadMsg: (t.attr("loadMsg") != undefined ? t.attr("loadMsg") : undefined), idField: t.attr("idField"), toolbar: t.attr("toolbar"), url: t.attr("url") });
	};

	var renderView = {
		render: function(target, container, frozen) {
			var opts = $.data(target, "nrietgrid").options;
			var rows = $.data(target, "nrietgrid").data.rows;
			var closFileds = getColumnFields(target, frozen);
			if(frozen) {
				if(!(opts.rownumbers || (opts.frozenColumns && opts.frozenColumns.length))) {
					return;
				}
			}
			var htmlAry = ["<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody>"];
			for(var i = 0; i < rows.length; i++) {
				var cls = (i % 2 && opts.striped) ? "class=\"nrietgrid-row-alt nrietgrid-row\"" : "class=\"nrietgrid-row\"";
				var rowStyle = opts.rowStyler ? opts.rowStyler.call(target, i, rows[i]) : "";
				var strStyle = rowStyle ? "style=\"" + rowStyle + "\"" : "";
				var trid = $.data(target, "nrietgrid").rowIdPrefix + "-" + (frozen ? 1 : 2) + "-" + i;
				//				console.log(trid);
				htmlAry.push("<tr id=\"" + trid + "\" nrietgrid-row-index=\"" + i + "\" " + cls + " " + strStyle + ">");
				htmlAry.push(this.renderRow.call(this, target, closFileds, frozen, i, rows[i]));
				htmlAry.push("</tr>");
			}
			htmlAry.push("</tbody></table>");
			$(container).html(htmlAry.join(""));
		},
		renderFooter: function(target, container, frozen) {
			var opts = $.data(target, "nrietgrid").options;
			var rows = $.data(target, "nrietgrid").footer || [];
			var cols = getColumnFields(target, frozen);
			var htmlAry = ["<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody>"];
			for(var i = 0; i < rows.length; i++) {
				htmlAry.push("<tr nrietgrid-row-index=\"" + i + "\">");
				htmlAry.push(this.renderRow.call(this, target, cols, frozen, i, rows[i]));
				htmlAry.push("</tr>");
			}
			htmlAry.push("</tbody></table>");
			$(container).html(htmlAry.join(""));
		},
		renderRow: function(target, fileds, frozen, rowIndex, rowData) {
			var opts = $.data(target, "nrietgrid").options;
			var cc = [];
			if(frozen && opts.rownumbers) {
				var tmpIndex = rowIndex + 1;
				if(opts.pagination) {
					tmpIndex += (opts.pageNumber - 1) * opts.pageSize;
				}
				cc.push("<td class=\"nrietgrid-td-rownumber\"><div class=\"nrietgrid-cell-rownumber\">" + tmpIndex + "</div></td>");
			}

			for(var i = 0; i < fileds.length; i++) {
				var findFiled = fileds[i];
				var col = getColOptions(target, findFiled);
				if(col) {
					var findStyler = col.styler ? (col.styler(rowData[findFiled], rowData, rowIndex) || "") : "";
					var strFindStyler = col.hidden ? "style=\"display:none;" + findStyler + "\"" : (findStyler ? "style=\"" + findStyler + "\"" : "");
					cc.push("<td field=\"" + findFiled + "\" " + strFindStyler + " class=\"" + col.cellClass + "\">");

					if(col.checkbox) {
						strFindStyler = "";
					} else {
						strFindStyler = findStyler;
						if(col.align) {
							strFindStyler += ";text-align:" + col.align + ";";
						}
						if(!opts.nowrap) {
							strFindStyler += ";white-space:normal;height:auto;";
						} else {
							if(opts.autoRowHeight) {
								strFindStyler += ";height:auto;";
							}
						}
					}
					if(col.boxWidth) {
						strFindStyler += "width:" + (col.boxWidth) + "px;";
					}
					cc.push("<div style=\"" + strFindStyler + "\" title=\"" + (rowData[findFiled] != undefined ? rowData[findFiled] : "") + "\"");
					if(col.checkbox) {
						cc.push("class=\"nrietgrid-cell-check ");
					} else {
						//						cc.push("class=\"nrietgrid-cell "+ col.cellClass); 
						cc.push(" class=\"nrietgrid-cell ");
					}
					cc.push("\">");
					if(col.checkbox) {
						cc.push("<input type=\"checkbox\" name=\"" + findFiled + "\" value=\"" + (rowData[findFiled] != undefined ? rowData[findFiled] : "") + "\"/>");
					} else {
						if(col.formatter) {
							cc.push(col.formatter(rowData[findFiled], rowData, rowIndex));
						} else {
							cc.push(rowData[findFiled]);
						}
					}
					cc.push("</div>");
					cc.push("</td>");
				}
			}
			return cc.join("");
		},
		refreshRow: function(target, rowIndex) {
			var rows = $.data(target, "nrietgrid").data.rows;
			this.updateRow.call(this, target, rowIndex, rows[rowIndex]);
		},
		updateRow: function(target, rowIndex, row) {
			var opts = $.data(target, "nrietgrid").options;
			var pnl = $.data(target, "nrietgrid").panel;
			var rows = $.data(target, "nrietgrid").data.rows;
			var tr = pnl.find("div.nrietgrid-body tr[nrietgrid-row-index=" + rowIndex + "]");
			for(var tmpfiled in row) {
				rows[rowIndex][tmpfiled] = row[tmpfiled];
				var td = tr.children("td[field=" + tmpfiled + "]");
				var cell = td.find("div.nrietgrid-cell");
				var col = getColOptions(target, tmpfiled);
				if(col) {
					var findStyler = col.styler ? col.styler(rows[rowIndex][tmpfiled], rows[rowIndex], rowIndex) : "";
					td.attr("style", findStyler || "");
					if(col.hidden) {
						td.hide();
					}
					if(col.formatter) {
						cell.html(col.formatter(rows[rowIndex][tmpfiled], rows[rowIndex], rowIndex));
					} else {
						cell.html(rows[rowIndex][tmpfiled]);
					}
				}
			}
			var findStyler = opts.rowStyler ? opts.rowStyler.call(target, rowIndex, rows[rowIndex]) : "";
			tr.attr("style", findStyler || "");
			fixRowHeight(target, rowIndex);
		},
		insertRow: function(target, rowIndex, row) {
			var opts = $.data(target, "nrietgrid").options;
			var data = $.data(target, "nrietgrid").data;
			var view = $.data(target, "nrietgrid").panel.children("div.nrietgrid-view");
			var view1 = view.children("div.nrietgrid-view1");
			var view2 = view.children("div.nrietgrid-view2");
			if(rowIndex == undefined || rowIndex == null) {
				rowIndex = data.rows.length;
			}
			if(rowIndex > data.rows.length) {
				rowIndex = data.rows.length;
			}
			for(var i = data.rows.length - 1; i >= rowIndex; i--) {
				view2.children("div.nrietgrid-body").find("tr[nrietgrid-row-index=" + i + "]").attr("nrietgrid-row-index", i + 1);
				var tr = view1.children("div.nrietgrid-body").find("tr[nrietgrid-row-index=" + i + "]").attr("nrietgrid-row-index", i + 1);
				if(opts.rownumbers) {
					tr.find("div.nrietgrid-cell-rownumber").html(i + 2);
				}
			}
			var frozenCols = getColumnFields(target, true);
			var fitCols = getColumnFields(target, false);
			var tr1 = "<tr nrietgrid-row-index=\"" + rowIndex + "\">" + this.renderRow.call(this, target, frozenCols, true, rowIndex, row) + "</tr>";
			var tr2 = "<tr nrietgrid-row-index=\"" + rowIndex + "\">" + this.renderRow.call(this, target, fitCols, false, rowIndex, row) + "</tr>";
			if(rowIndex >= data.rows.length) {
				var view1BodyInner = view1.children("div.nrietgrid-body").children("div.nrietgrid-body-inner");
				var view2Body = view2.children("div.nrietgrid-body");
				if(data.rows.length) {
					view1BodyInner.find("tr:last[nrietgrid-row-index]").after(tr1);
					view2Body.find("tr:last[nrietgrid-row-index]").after(tr2);
				} else {
					view1BodyInner.html("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody>" + tr1 + "</tbody></table>");
					view2Body.html("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody>" + tr2 + "</tbody></table>");
				}
			} else {
				view1.children("div.nrietgrid-body").find("tr[nrietgrid-row-index=" + (rowIndex + 1) + "]").before(tr1);
				view2.children("div.nrietgrid-body").find("tr[nrietgrid-row-index=" + (rowIndex + 1) + "]").before(tr2);
			}
			data.total += 1;
			data.rows.splice(rowIndex, 0, row);
			this.refreshRow.call(this, target, rowIndex);
		},
		deleteRow: function(target, rowIndex) {
			var opts = $.data(target, "nrietgrid").options;
			var data = $.data(target, "nrietgrid").data;
			var view = $.data(target, "nrietgrid").panel.children("div.nrietgrid-view");
			var view1 = view.children("div.nrietgrid-view1");
			var view2 = view.children("div.nrietgrid-view2");
			view1.children("div.nrietgrid-body").find("tr[nrietgrid-row-index=" + rowIndex + "]").remove();
			view2.children("div.nrietgrid-body").find("tr[nrietgrid-row-index=" + rowIndex + "]").remove();
			for(var i = rowIndex + 1; i < data.rows.length; i++) {
				view2.children("div.nrietgrid-body").find("tr[nrietgrid-row-index=" + i + "]").attr("nrietgrid-row-index", i - 1);
				var tr = view1.children("div.nrietgrid-body").find("tr[nrietgrid-row-index=" + i + "]").attr("nrietgrid-row-index", i - 1);
				if(opts.rownumbers) {
					tr.find("div.nrietgrid-cell-rownumber").html(i);
				}
			}
			data.total -= 1;
			data.rows.splice(rowIndex, 1);
		},
		onBeforeRender: function(target, rows) {},
		onAfterRender: function(target) {
			var opts = $.data(target, "nrietgrid").options;
			if(opts.showFooter) {
				var footer = $.data(target, "nrietgrid").panel.find("div.nrietgrid-footer");
				footer.find("div.nrietgrid-cell-rownumber,div.nrietgrid-cell-check").css("visibility", "hidden");
			}
		}
	};

	var editorsObj = {
		text: {
			init: function(container, options) {
				var gridInput = $("<input type=\"text\" class=\"nrietgrid-editable-input\">").appendTo(container);
				var height = container.closest("td.nrietgrid-cell-editing").height();
				var width = container.closest("td.nrietgrid-cell-editing").width();
				gridInput.css({ "height": height, "width": width });
				return gridInput;
			},
			getValue: function(target) {
				return $(target).val();
			},
			setValue: function(target, value) {
				$(target).val(value);
			},
			resize: function(target, width) {
				var targetObj = $(target);
				if($.boxModel == true) {
					targetObj.css({ "width": width - (targetObj.outerWidth() - targetObj.width()) + "px" });
				} else {
					targetObj.css({ "width": width + "px" });
				}
			}
		},
		textarea: {
			init: function(container, options) {
				var gridTextAreaObj = $("<textarea class=\"nrietgrid-editable-input datagrid-editable-input\" readyonly='readyonly'></textarea>").appendTo(container);
				var height = container.closest("td.nrietgrid-cell-editing").height();
				var width = container.closest("td.nrietgrid-cell-editing").width();
				gridTextAreaObj.css({ "height": height, "width": width });
				return gridTextAreaObj;
			},
			getValue: function(target) {
				return $(target).val();
			},
			setValue: function(target, value) {
				$(target).val(value);
			},
			resize: function(target, width) {
				var targetObj = $(target);
				if($.boxModel == true) {
					targetObj.css({ "width": width - (targetObj.outerWidth() - targetObj.width()) + "px" });
				} else {
					targetObj.css({ "width": width + "px" });
				}
			}
		},
		checkbox: {
			init: function(container, options) {
				var gridCheckBoxObj = $("<input type=\"checkbox\">").appendTo(container);
				gridCheckBoxObj.val(options.on);
				gridCheckBoxObj.attr("offval", options.off);
				return gridCheckBoxObj;
			},
			getValue: function(target) {
				if($(target).attr("checked")) {
					return $(target).val();
				} else {
					return $(target).attr("offval");
				}
			},
			setValue: function(target, value) {
				if($(target).val() == value) {
					$(target).attr("checked", true);
				} else {
					$(target).attr("checked", false);
				}
			}
		},
		numberbox: {
			init: function(container, options) {
				var gridNumBoxObj = $("<input type=\"text\" class=\"nrietgrid-editable-input datagrid-editable-input\">").appendTo(container);
				var height = container.closest("td.nrietgrid-cell-editing").height();
				var width = container.closest("td.nrietgrid-cell-editing").width();
				gridNumBoxObj.css({ "height": height, "width": width });
				gridNumBoxObj.numberbox(options);
				return gridNumBoxObj;
			},
			destroy: function(target) {
				$(target).numberbox("destroy");
			},
			getValue: function(target) {
				return $(target).numberbox("getValue");
			},
			setValue: function(target, value) {
				value = value && value != "" && value != "-" ? parseFloat(value) : value;
				$(target).val(value);
				$(target).numberbox("setValue", value);
			},
			resize: function(target, width) {
				var targetObj = $(target);
				if($.boxModel == true) {
					targetObj.css({ "width": width - (targetObj.outerWidth() - targetObj.width()) + "px" });
				} else {
					targetObj.css({ "width": width + "px" });
				}
			}
		},
		validatebox: {
			init: function(container, options) {
				var gridvalidateBoxObj = $("<input type=\"text\" class=\"nrietgrid-editable-input datagrid-editable-input\">").appendTo(container);
				var height = container.closest("td.nrietgrid-cell-editing").height();
				var width = container.closest("td.nrietgrid-cell-editing").width();
				gridvalidateBoxObj.css({ "height": height, "width": width });
				gridvalidateBoxObj.validatebox(options);
				return gridvalidateBoxObj;
			},
			destroy: function(target) {
				$(target).validatebox("destroy");
			},
			getValue: function(target) {
				return $(target).val();
			},
			setValue: function(target, value) {
				$(target).val(value);
			},
			resize: function(target, width) {
				var targetObj = $(taget);
				if($.boxModel == true) {
					targetObj.css({ "width": width - (targetObj.outerWidth() - targetObj.width()) + "px" });
				} else {
					targetObj.css({ "width": width + "px" });
				}
			}
		},
		datebox: {
			init: function(container, options) {
				var gridDateBoxObj = $("<input type=\"text\">").appendTo(container);
				var height = container.closest("td.nrietgrid-cell-editing").height();
				var width = container.closest("td.nrietgrid-cell-editing").width();
				gridDateBoxObj.css({ "height": height, "width": width });
				gridDateBoxObj.datebox(options);
				return gridDateBoxObj;
			},
			destroy: function(target) {
				$(target).datebox("destroy");
			},
			getValue: function(target) {
				return $(target).datebox("getValue");
			},
			setValue: function(target, value) {
				$(target).datebox("setValue", value);
			},
			resize: function(target, width) {
				$(target).datebox("resize", width);
			}
		},
		combobox: {
			init: function(container, options) {
				var gridcomboxObj = $("<input type=\"text\">").appendTo(container);
				var height = container.closest("td.nrietgrid-cell-editing").height();
				var width = container.closest("td.nrietgrid-cell-editing").width();
				gridcomboxObj.css({ "height": height, "width": width });
				gridcomboxObj.combobox(options || {});
				return gridcomboxObj;
			},
			destroy: function(target) {
				$(target).combobox("destroy");
			},
			getValue: function(target) {
				return $(target).combobox("getValue");
			},
			setValue: function(target, value) {
				$(target).combobox("setValue", value);
			},
			resize: function(target, width) {
				$(target).combobox("resize", width);
			}
		},
		nselect: {
			init: function(container, options) {
				var height = container.closest("td.nrietgrid-cell-editing").height();
				var width = container.closest("td.nrietgrid-cell-editing").width();
				var oldval = container.closest("div.nrietgrid-editable").attr("oldval");
				var span = $("<span name=\"cellhtm\">" + oldval + "</span>").appendTo(container);
				span.css({ "height": height, "width": width });
				var pnl = $("<div style=\"position:absolute;\"><div class=\"nrietgrid-nselect-panel\"></div></div>").appendTo(container);
				pnl.find(".nrietgrid-nselect-panel").css({
					"top": 0,
					"left": 0,
					"width": width + "px"
				}).data("container", container);
				/*
				var pageleft = pnl[0].style.pageX;
				var pagetop = pnl[0].style.pageY;
				pnl.appendTo($("body")).css({
					"top": pagetop,
					"left": pageleft
				})
				*/
				
				var selectBoxObj = pnl.find(".nrietgrid-nselect-panel");
				$.data(selectBoxObj[0], "container", { container: container });
				
				var pageleft = getLeft(pnl[0]);
				var pagetop = getTop(pnl[0]);
				pnl.appendTo($("div.nrietpanel.nrietgrid")).css({
					"top": pagetop,
					"left": pageleft,
					"z-index": 99999
				})
				data = options.ndata;
				var listyle = options.minwidth ? "style=\"width:" + options.minwidth + "px;\" " : "";
				if(data) {
					selectBoxObj.append($("<ul>"));
					for(var i = 0; i < data.length; i++) {
						var opcode = data[i]["key"];
						var opval = data[i]["text"];
						var type = data[i]["type"];

						var li = $("<li channel=\"" + opcode + "\" optype=\"" + type + "\" class=\"nrietgrid-nselect-li\" title=\"" + opval + "\" " + listyle + ">" + opval + "</li>").appendTo(selectBoxObj.find("ul"));
					}
				}

				selectBoxObj.find("li.nrietgrid-nselect-li").unbind(".nrietgrid").bind("click.nrietgrid", function() {
					var target = $(this).closest("div.nrietgrid-nselect-panel");
					var $div = $.data(target[0], "container").container.closest("div.nrietgrid-editable");
					var opcode = $(this).attr("channel");
					var opval = $(this).html();
					var type = $(this).attr("optype");
					if(type > 0) {
						if($div.attr("key") && !($div.attr("optype") > 0)) {
							$div.attr({
								"key": $div.attr("key") + opcode,
								"oldval": $div.attr("oldval") + opval,
								"optype": type
							});
							$div.find("span[name=\"cellhtm\"]").html($div.attr("oldval"));
						}
					} else {
						if($div.attr("optype") > 0) {
							$div.attr({
								"key": $div.attr("key") + opcode,
								"oldval": $div.attr("oldval") + opval,
								"optype": type
							});
							$div.find("span[name=\"cellhtm\"]").html($div.attr("oldval"));
						} else {
							$div.attr({
								"key": opcode,
								"oldval": opval,
								"optype": type
							});
							$div.find("span[name=\"cellhtm\"]").html(opval);
						}
					}
				});

				return selectBoxObj;
			},
			getValue: function(target) {
				return $.data(target[0], "container").container.closest("div.nrietgrid-editable").find("span[name=\"cellhtm\"]").html();
			},
			setValue: function(target, value) {
				$.data(target[0], "container").container.closest("div.nrietgrid-editable").find("span[name=\"cellhtm\"]").html(value)
			},
			getValueMap: function(target) {
				var $div = $.data(target[0], "container").container.closest("div.nrietgrid-editable");
				return {
					"key": $div.attr("key"),
					"text": $div.attr("oldval")
				};
			},
			setValueSetMap: function(target, value) {
				var $div = $.data(target[0], "container").container.closest("div.nrietgrid-editable");
				$div.attr({
					"key": value.key,
					"oldval": value.text,
					"optype": value.type
				});
				$div.find("span[name=\"cellhtm\"]").html(value.text);
			},
			resize: function(target, width) {

			}

		},
		npanelbox: {
			init: function(container, options) {
				$("div.nrietgrid-npanelbox-panel").parent().remove();
				var height = container.closest("td.nrietgrid-cell-editing").height();
				var width = container.closest("td.nrietgrid-cell-editing").width();
				var oldval = container.closest("div.nrietgrid-editable").attr("oldval");
				var span = $("<span name=\"cellhtm\">" + oldval + "</span>").appendTo(container);
				span.css({ "height": height, "width": width });
				var pnl = $("<div style=\"position:absolute;\"><div class=\"nrietgrid-npanelbox-panel\"></div></div>").appendTo(container);
				var selectBoxObj = pnl.find(".nrietgrid-npanelbox-panel");
				$.data(selectBoxObj[0], "container", { container: container });

				var pageleft = getLeft(pnl[0]);
				var pagetop = getTop(pnl[0]);
				if(pageleft > 980){
					pnl.find(".nrietgrid-npanelbox-panel").css({
						"top": -height + "px",
						"right": 3
					});
				}else{
					pnl.find(".nrietgrid-npanelbox-panel").css({
						"top": -height + "px",
						"left": width + 3 + "px"
					});
				}
				
				var outer = $(container).closest(".tf-grid");
				if(outer.length == 0 || outer[0].scrollTop == 0){
					outer = $("#iSouth");
				}
				if(outer && outer.length > 0){
					pagetop = pagetop - outer[0].scrollTop;
				}
				
				pnl.appendTo($(container).closest('div.nrietpanel.nrietgrid')).css({
					"top": pagetop,
					"left": pageleft,
					"z-index": 99999
				});
				data = options.ndata;
				if(data) {
					selectBoxObj.append($("<ul>"));
					for(var i = 0; i < data.length; i++) {
						var opcode = data[i]["key"];
						var opval = data[i]["text"];
						var type = data[i]["type"];

						var li = $("<li channel=\"" + opcode + "\" optype=\"" + type + "\" class=\"nrietgrid-npanelbox-li\" title=\"" + opval + "\">" + opval + "</li>").appendTo(selectBoxObj.find("ul"));
					}

					var num = parseInt(Math.sqrt(data.length));
					if(data.length > 6 && data.length <= 30){
						num = 3;//暂时固定4列
					}else if(data.length > 30){
						num = 4;
					}else{
						num = 2; 
					}
					num = num > 8 ? 8 : (data.length > num * num ? (num + 1) : num);

					var liwidth = selectBoxObj.find("li.nrietgrid-npanelbox-li").width();
					selectBoxObj.css({
						"width": num * liwidth + num + 1 + "px"
					})
				}

				selectBoxObj.find("li.nrietgrid-npanelbox-li").unbind(".nrietgrid").bind("click.nrietgrid", function() {
					var target = $(this).closest("div.nrietgrid-npanelbox-panel");
					var $div = $.data(target[0], "container").container.closest("div.nrietgrid-editable");
					var opcode = $(this).attr("channel");
					var opval = $(this).html();
					var type = $(this).attr("optype");
					if(type > 0) {
						if($div.attr("key") && !($div.attr("optype") > 0)) {
							$div.attr({
								"key": $div.attr("key") + opcode,
								"oldval": $div.attr("oldval") + opval,
								"optype": type
							});
							$div.find("span[name=\"cellhtm\"]").html($div.attr("oldval"));
						}
					} else {
						if($div.attr("optype") > 0) {
							$div.attr({
								"key": $div.attr("key") + opcode,
								"oldval": $div.attr("oldval") + opval,
								"optype": type
							});
							$div.find("span[name=\"cellhtm\"]").html($div.attr("oldval"));
						} else {
							$div.attr({
								"key": opcode,
								"oldval": opval,
								"optype": type
							});
							$div.find("span[name=\"cellhtm\"]").html(opval);
						}
					}
					target.hide();
					clearEdit(target);
				});
				
				return selectBoxObj;
			},
			destroy: function(target) {
				$("div.nrietgrid-npanelbox-panel").parent().remove();
			},
			getValue: function(target) {
				return $.data(target[0], "container").container.closest("div.nrietgrid-editable").find("span[name=\"cellhtm\"]").html();
			},
			setValue: function(target, value) {
				$.data(target[0], "container").container.closest("div.nrietgrid-editable").find("span[name=\"cellhtm\"]").html(value)
			},
			getValueMap: function(target) {
				var $div = $.data(target[0], "container").container.closest("div.nrietgrid-editable");
				return {
					"key": $div.attr("key"),
					"text": $div.attr("oldval")
				};
			},
			setValueSetMap: function(target, value) {
				var $div = $.data(target[0], "container").container.closest("div.nrietgrid-editable");
				$div.attr({
					"key": value.key,
					"oldval": value.text,
					"optype": value.type
				});
				$div.find("span[name=\"cellhtm\"]").html(value.text);
			},
			resize: function(target, width) {
				var element = $(".nrietgrid-npanelbox-panel");
				var offsetTop = element.offset().top;
				var elementHeight = element.height();
				var docHeight = $(document).height();
				var elementTop = parseInt(element.css("top"));
				var parentTop = parseInt(element.parent().css("top"));
				if (offsetTop + elementHeight >= docHeight) {
					parentTop -= (elementHeight+elementTop);
				}
				element.parent().css({"top": parentTop})
			}
		},
		numbox: {
			init: function(container, options) {
				var gridNumBoxObj = $("<input type=\"text\" class=\"nrietgrid-editable-input datagrid-editable-input\">").appendTo(container);
				var height = container.closest("td.nrietgrid-cell-editing").height();
				var width = container.closest("td.nrietgrid-cell-editing").width();
				gridNumBoxObj.css({ "height": height, "width": width });
				gridNumBoxObj.numberbox(options);
				var plus = $("<div class=\"nrietgrid-a-plus\">+</div>").appendTo(gridNumBoxObj.parent().children("span"));
				var sub = $("<div class=\"nrietgrid-a-sub\">-</div>").appendTo(gridNumBoxObj.parent().children("span"));
				var dvalue = options && options.dvalue ? options.dvalue : 1;
				plus.unbind(".nrietgrid").bind("click.nrietgrid", function() {
					var numBox = $(this).closest("td").find("input.nrietgrid-editable-input");
					var plusVal = Number(numBox.numberbox("getValue")) + Number(dvalue);
					var old = $(this).prev().val();
					var filed = $(this).parent().closest("div").closest("td").attr("field");
					var oVal = "";
					if(filed.indexOf("MAX") > -1){
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
						if(oVal !== "" && oVal > plusVal){
							alert("最高气温不能小于最低气温！");
							$(this).val(old ? old : "");
							return;
						}
					}else{
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
						if(oVal !== "" && oVal < plusVal){
							alert("最低气温不能大于最高气温！");
							$(this).val(old ? old : "");
							return;
						}	
					}
					if(COMMON_TOP && old != plusVal){
						var flag = COMMON_TOP.validateTemp(filed, plusVal);
						if(!flag){
							$(this).val(old ? old : "");
							return;
						}
					}
					numBox.numberbox("setValue", plusVal);
				});

				sub.unbind(".nrietgrid").bind("click.nrietgrid", function() {
					var numBox = $(this).closest("td").find("input.nrietgrid-editable-input");
					var subVal = numBox.numberbox("getValue") - Number(dvalue);
					var old = $(this).prev().prev().val();
					var filed = $(this).parent().closest("div").closest("td").attr("field");
					var oVal = "";
					if(filed.indexOf("MAX") > -1){
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
						if(oVal && oVal !== "" && oVal > subVal){
							alert("最高气温不能小于最低气温！");
							$(this).val(old ? old : "");
							return;
						}
					}else{
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
						if(oVal && oVal !== "" && oVal < subVal){
							alert("最低气温不能大于最高气温！");
							$(this).val(old ? old : "");
							return;
						}	
					}
					if(COMMON_TOP && old != subVal){
						var flag = COMMON_TOP.validateTemp(filed, subVal);
						if(!flag){
							$(this).val(old ? old : "");
							return;
						}
					}
					numBox.numberbox("setValue", subVal);
				});
				
				plus.prev().prev().bind("input mouseup", function(){
					var val = $(this).val() == "0." || $(this).val() == "-" || $(this).val() == "" ? "" : Number($(this).val());
					var old = $(this).next().val();
					var filed = $(this).closest("div").closest("td").attr("field");
					var oVal = "";
					if(filed.indexOf("MAX") > -1 && val !== ""){
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
						if(oVal !== "" && oVal > val){
							alert("最高气温不能小于最低气温！");
							$(this).val(old ? old : "");
							return;
						}
					}else if(val !== ""){
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
						if(oVal !== "" && oVal < val){
							alert("最低气温不能大于最高气温！");
							$(this).val(old ? old : "");
							return;
						}	
					}
					if(COMMON_TOP && val != old){
						var flag = COMMON_TOP.validateTemp(filed, val);
						if(!flag){
							$(this).val(old ? old : "");
						}
					}
					$(this).next().val(val);
				});
				
				var scrollfun = function(e) {
					e = e || window.event;
					e.stopPropagation();
					e.preventDefault();
					if(e.wheelDelta) {
						if(e.wheelDelta > 0) {
							var upVal =  Number(gridNumBoxObj.numberbox("getValue")) + Number(dvalue);
							var old = gridNumBoxObj.next().find(".textbox-value").val();
							var filed = gridNumBoxObj.closest("div").closest("td").attr("field");
							var oVal = "";
							if(filed.indexOf("MAX") > -1){
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
								if(oVal !== "" && oVal > upVal){
									alert("最高气温不能小于最低气温！");
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}
							}else{
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
								if(oVal !== "" && oVal < upVal){
									alert("最低气温不能大于最高气温！");
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}	
							}
							if(COMMON_TOP && upVal != old){
								var flag = COMMON_TOP.validateTemp(filed, upVal);
								if(!flag){
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}
							}
							gridNumBoxObj.numberbox("setValue", upVal);
						} else {
							var downVal =  Number(gridNumBoxObj.numberbox("getValue")) - Number(dvalue);
							var old = gridNumBoxObj.next().find(".textbox-value").val();
							var filed = gridNumBoxObj.closest("div").closest("td").attr("field");
							var oVal = "";
							if(filed.indexOf("MAX") > -1){
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
								if(oVal !== "" && oVal > downVal){
									alert("最高气温不能小于最低气温！");
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}
							}else{
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
								if(oVal !== "" && oVal < downVal){
									alert("最低气温不能大于最高气温！");
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}	
							}
							if(COMMON_TOP && downVal != old){
								var flag = COMMON_TOP.validateTemp(filed, downVal);
								if(!flag){
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}
							}
							gridNumBoxObj.numberbox("setValue", downVal);
						}
					}
					if(e.detail) {
						if(e.detail > 0) {
							var upVal =  Number(gridNumBoxObj.numberbox("getValue")) + Number(dvalue);
							var old = gridNumBoxObj.next().find(".textbox-value").val();
							var filed = gridNumBoxObj.closest("div").closest("td").attr("field");
							var oVal = "";
							if(filed.indexOf("MAX") > -1){
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
								if(oVal !== "" && oVal > upVal){
									alert("最高气温不能小于最低气温！");
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}
							}else{
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
								if(oVal !== "" && oVal < upVal){
									alert("最低气温不能大于最高气温！");
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}	
							}
							if(COMMON_TOP && upVal != old){
								var flag = COMMON_TOP.validateTemp(filed, upVal);
								if(!flag){
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}
							}
							gridNumBoxObj.numberbox("setValue", upVal);
						} else {
							var downVal =  Number(gridNumBoxObj.numberbox("getValue")) - Number(dvalue);
							var old = gridNumBoxObj.next().find(".textbox-value").val();
							var filed = gridNumBoxObj.closest("div").closest("td").attr("field");
							var oVal = "";
							if(filed.indexOf("MAX") > -1){
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
								if(oVal !== "" && oVal > downVal){
									alert("最高气温不能小于最低气温！");
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}
							}else{
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
								if(oVal !== "" && oVal < downVal){
									alert("最低气温不能大于最高气温！");
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}	
							}
							if(COMMON_TOP && downVal != old){
								var flag = COMMON_TOP.validateTemp(filed, downVal);
								if(!flag){
									gridNumBoxObj.find(".textbox-value").val(old ? old : "");
									return;
								}
							}
							gridNumBoxObj.numberbox("setValue", downVal);
						}
					}
				};

				if(document.addEventListener) {
					document.addEventListener("DOMMouseScroll", scrollfun, false);
				}

				window.onmousewheel = document.onmousewheel = scrollfun;
				return gridNumBoxObj;
			},
			destroy: function(target) {
				$(target).numberbox("destroy");
				if(document.addEventListener) {
					document.addEventListener("DOMMouseScroll", function() {}, false);
				}
				window.onmousewheel = document.onmousewheel = function() {};
			},
			getValue: function(target) {
				return $.isNumeric($(target).numberbox("getValue")) ? parseFloat($(target).numberbox("getValue")) : parseFloat($(target).numberbox("getValue"));
			},
			setValue: function(target, value) {
				$(target).numberbox("setValue", value && value != "" && value != "-" ? parseFloat(value) : value);
			},
			resize: function(target, width) {
				var targetObj = $(target);
				if($.boxModel == true) {
					targetObj.css({ "width": width - (targetObj.outerWidth() - targetObj.width()) + "px" });
				} else {
					targetObj.css({ "width": width + "px" });
				}
			}
		},
		panelnumbox: {
			init: function(container, options) {
				var height = container.closest("td.nrietgrid-cell-editing").height();
				var width = container.closest("td.nrietgrid-cell-editing").width();
				var oldval = container.closest("div.nrietgrid-editable").attr("oldval");
				if($.isNumeric(oldval)){
					oldval = parseFloat(oldval);
				}
				var span = $("<span name=\"cellhtm\">" + oldval + "</span>").appendTo(container);
				span.css({ "height": height, "width": width });
				var pnl = $("<div style=\"position:absolute;\"><div class=\"nrietgrid-panelnumbox-panel\"></div></div>").appendTo(container);
				
				var pageleft = pnl[0].offsetLeft + width;//getLeft(pnl[0]);
				var pagetop = pnl[0].offsetTop + height;//getTop(pnl[0]);
				var tableHight = container.closest(".nrietgrid-view2").height();
				var tableWidth = container.closest(".nrietgrid-view2").width();
				var pnlHeight = pnl.find(".nrietgrid-panelnumbox-panel").height();
				var pnlWidth = pnl.find(".nrietgrid-panelnumbox-panel").width() + 20;
				if(tableWidth - pageleft < pnlWidth){
					pnl.find(".nrietgrid-panelnumbox-panel").css({
						"top": -height,
						"right": 0
					});
				}else{
					pnl.find(".nrietgrid-panelnumbox-panel").css({
						"top": -height,
						"left": width
					});
				}
				
				if(pagetop - tableHight > -10){
					pnl.find(".nrietgrid-panelnumbox-panel").css({
						"top": -pnlHeight
					});
				}
				var operate = $("<div style=\"height: 30px; line-height: 30px; margin-bottom: 10px;\">" +
					"<input type=\"radio\" id=\"oprate-cell\" name=\"panelnumbox-operate\" value=\"cell\" checked />" +
					"<label for=\"oprate-cell\">单元格</label>" +
					"<input type=\"radio\" id=\"oprate-column\" name=\"panelnumbox-operate\" value=\"column\" />" +
					"<label for=\"oprate-column\">整列</label></div>").appendTo(pnl.find(".nrietgrid-panelnumbox-panel"));
				var gridNumBoxObj = $("<input type=\"text\" class=\"nrietgrid-editable-input datagrid-editable-input\">").appendTo(pnl.find(".nrietgrid-panelnumbox-panel"));
				if(height > 40){
					height = 35;
				}
				gridNumBoxObj.css({ "height": height, "width": width });
				gridNumBoxObj.numberbox(options);
				var plus = $("<div class=\"nrietgrid-a-plus plus-sub-newclass\">+</div>").appendTo(gridNumBoxObj.parent().children("span")).height(height / 2);
				var sub = $("<div class=\"nrietgrid-a-sub plus-sub-newclass\">-</div>").appendTo(gridNumBoxObj.parent().children("span")).height(height / 2);
				
				//输入框“+”按钮
				plus.unbind(".nrietgrid").bind("click.nrietgrid", function() {
					var numBox = $(this).closest("td").find("input.nrietgrid-editable-input");
					var val = 1 - (-numBox.numberbox("getValue"));
					var old = $(this).prev().val();
					var filed = $(this).closest(".nrietgrid-cell-editing").attr("field");
					var oVal = "";
					if(filed.indexOf("MAX") > -1){
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
						if(oVal !== "" && oVal > val){
							alert("最高气温不能小于最低气温！");
							$(this).val(old ? old : "");
							return;
						}
					}else{
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
						if(oVal !== "" && oVal < val){
							alert("最低气温不能大于最高气温！");
							$(this).val(old ? old : "");
							return;
						}	
					}
					if(COMMON_TOP && old != val){
						var flag = COMMON_TOP.validateTemp(filed, val);
						if(!flag){
							$(this).val(old ? old : "");
							return;
						}
					}
					numBox.numberbox("setValue", val);
				});
				
				//输入框“-”按钮
				sub.unbind(".nrietgrid").bind("click.nrietgrid", function() {
					var numBox = $(this).closest("td").find("input.nrietgrid-editable-input");
					var subVal = numBox.numberbox("getValue") - 1;
					var old = $(this).prev().prev().val();
					var filed = $(this).closest(".nrietgrid-cell-editing").attr("field");
					var oVal = "";
					if(filed.indexOf("MAX") > -1){
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
						if(oVal !== "" && oVal > subVal){
							alert("最高气温不能小于最低气温！");
							$(this).val(old ? old : "");
							return;
						}
					}else{
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
						if(oVal !== "" && oVal < subVal){
							alert("最低气温不能大于最高气温！");
							$(this).val(old ? old : "");
							return;
						}	
					}
					if(COMMON_TOP && old != subVal){
						var flag = COMMON_TOP.validateTemp(filed, subVal);
						if(!flag){
							$(this).val(old ? old : "");
							return;
						}
					}
					numBox.numberbox("setValue", subVal);
				});
				
				//输入框键盘输入
				plus.prev().prev().bind("input mouseup", function(){
					var val = $(this).val() == "0." || $(this).val() == "-" || $(this).val() == "" ? "" : Number($(this).val());
					var old = $(this).next().val();
					var field = $(this).closest(".nrietgrid-cell-editing").attr("field");
					var oVal = "";
					if(field.indexOf("MAX") > -1 && val != ""){
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
						if(oVal !== "" && oVal > val){
							alert("最高气温不能小于最低气温！");
							$(this).val(old ? old : "");
							return;
						}
					}else if(val != ""){
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
						if(oVal !== "" && oVal < val){
							alert("最低气温不能大于最高气温！");
							$(this).val(old ? old : "");
							return;
						}	
					}
					if(COMMON_TOP && val != old){
						var flag = COMMON_TOP.validateTemp(field, val);
						if(!flag){
							$(this).val(old ? old : "");
						}
					}
					$(this).next().val(val);
				});

				var plusbtn = $("<div class=\"nrietgrid-btn-plus nrietgrid-btn\">+</div>").appendTo(pnl.find(".nrietgrid-panelnumbox-panel")).height(height - 2);
				var subbtn = $("<div class=\"nrietgrid-btn-sub nrietgrid-btn\">-</div>").appendTo(pnl.find(".nrietgrid-panelnumbox-panel")).height(height - 2);
				var setbtn = $("<div class=\"nrietgrid-btn-set nrietgrid-btn\">=</div>").appendTo(pnl.find(".nrietgrid-panelnumbox-panel")).height(height - 2);

				var precision = options && options.precision ? options.precision : 0;
				plusbtn.unbind(".nrietgrid").bind("click.nrietgrid", function() {
					var op = $(this).closest("div.nrietgrid-panelnumbox-panel").find("input[type='radio'][name='panelnumbox-operate']:checked").val();
					var numBox = $(this).closest("div.nrietgrid-panelnumbox-panel").find("input.nrietgrid-editable-input");
					var val = (Number(container.closest("div.nrietgrid-editable").attr("oldval")) + Number(numBox.numberbox("getValue"))).toFixed(precision);
					var field = container.closest("div.nrietgrid-editable").closest("td").attr("field");
					var oVal = "";
					if(field.indexOf("MAX") > -1){
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
						if(oVal !== "" && oVal > parseFloat(val)){
							alert("最高气温不能小于最低气温！");
							return;
						}
					}else{
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
						if(oVal !== "" && oVal < parseFloat(val)){
							alert("最低气温不能大于最高气温！");
							return;
						}	
					}
					if(COMMON_TOP){
						var flag = COMMON_TOP.validateTemp(field, val);
						if(!flag){
							return;
						}
					}
					if("column" == op) {
						tds = container.closest("div.nrietgrid-editable").closest("table").find("td[field='" + field + "']");
						var target = tds.closest("div.nrietgrid-view").children(":hidden").not("style").last().get(0);
						var rows = $.data(target, "nrietgrid").data.rows;
						for(var i = 0; i < rows.length; i++) {
							rows[i][field] = (Number(rows[i][field] || 0) + Number(numBox.numberbox("getValue"))).toFixed(precision);
							if(tds.find("div.nrietgrid-cell").eq(i).find('table').length == 0) {
								tds.find("div.nrietgrid-cell").eq(i).html(rows[i][field]);
							}
						}
						container.closest("div.nrietgrid-editable").attr({ "oldval": val });
						container.find("span[name=\"cellhtm\"]").html(val);
					} else {
						container.closest("div.nrietgrid-editable").attr({ "oldval": val });
						container.find("span[name=\"cellhtm\"]").html(val);
					}
				});

				subbtn.unbind(".nrietgrid").bind("click.nrietgrid", function() {
					var op = $(this).closest("div.nrietgrid-panelnumbox-panel").find("input[type='radio'][name='panelnumbox-operate']:checked").val();
					var numBox = $(this).closest("div.nrietgrid-panelnumbox-panel").find("input.nrietgrid-editable-input");
					var val = (Number(container.closest("div.nrietgrid-editable").attr("oldval")) - Number(numBox.numberbox("getValue"))).toFixed(precision);;
					var field = container.closest("div.nrietgrid-editable").closest("td").attr("field");
					var oVal = "";
					if(field.indexOf("MAX") > -1){
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
						if(oVal !== "" && oVal > parseFloat(val)){
							alert("最高气温不能小于最低气温！");
							return;
						}
					}else{
						oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
						if(oVal !== "" && oVal < parseFloat(val)){
							alert("最低气温不能大于最高气温！");
							return;
						}	
					}
					if(COMMON_TOP){
						var flag = COMMON_TOP.validateTemp(field, val);
						if(!flag){
							return;
						}
					}
					if("column" == op) {
						tds = container.closest("div.nrietgrid-editable").closest("table").find("td[field='" + field + "']");
						tds.find("div.nrietgrid-cell").not("div.nrietgrid-editable").html(val);
						var target = tds.closest("div.nrietgrid-view").children(":hidden").not("style").last().get(0);
						var rows = $.data(target, "nrietgrid").data.rows;
						for(var i = 0; i < rows.length; i++) {
							rows[i][field] = (Number(rows[i][field] || 0) - Number(numBox.numberbox("getValue")));//.toFixed(precision);
							if(tds.find("div.nrietgrid-cell").eq(i).find('table').length == 0) {
								tds.find("div.nrietgrid-cell").eq(i).html(rows[i][field]);
							}
						}
						container.closest("div.nrietgrid-editable").attr({ "oldval": val });
						container.find("span[name=\"cellhtm\"]").html(val);
					} else {
						container.closest("div.nrietgrid-editable").attr({ "oldval": val });
						container.find("span[name=\"cellhtm\"]").html(val);
					}
				});
				setbtn.unbind(".nrietgrid").bind("click.nrietgrid", function() {
					var op = $(this).closest("div.nrietgrid-panelnumbox-panel").find("input[type='radio'][name='panelnumbox-operate']:checked").val();
					var numBox = $(this).closest("div.nrietgrid-panelnumbox-panel").find("input.nrietgrid-editable-input");

					if("column" == op) {
						var field = container.closest("div.nrietgrid-editable").closest("td").attr("field");
						tds = container.closest("div.nrietgrid-editable").closest("table").find("td[field='" + field + "']");
						tds.find("div.nrietgrid-cell").not("div.nrietgrid-editable").html(parseFloat(numBox.numberbox("getValue")));
						var target = tds.closest("div.nrietgrid-view").children(":hidden").not("style").last().get(0);
						var rows = $.data(target, "nrietgrid").data.rows;
						for(var i = 0; i < rows.length; i++) {
							rows[i][field] = parseFloat(numBox.numberbox("getValue"));
						}
						container.closest("div.nrietgrid-editable").attr({ "oldval": parseFloat(numBox.numberbox("getValue")) });
						container.find("span[name=\"cellhtm\"]").html(parseFloat(numBox.numberbox("getValue")));
					} else {
						container.closest("div.nrietgrid-editable").attr({ "oldval": parseFloat(numBox.numberbox("getValue")) });
						container.find("span[name=\"cellhtm\"]").html(parseFloat(numBox.numberbox("getValue")));
					}
				});
				var scrollfun = function(e) {
					e = e || window.event;
					e.stopPropagation();
					e.preventDefault();
					if(e.wheelDelta) {
						if(e.wheelDelta > 0) {
							var getVal = gridNumBoxObj.numberbox("getValue") == "" ? 0 : parseFloat(gridNumBoxObj.numberbox("getValue"));
							var upVal =  getVal + 1;
							var old = gridNumBoxObj.next().find(".textbox-value").val();
							var field = gridNumBoxObj.closest(".nrietgrid-cell-editing").attr("field");
							var oVal = "";
							if(field.indexOf("MAX") > -1){
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
								if(oVal !== "" && oVal > upVal){
									alert("最高气温不能小于最低气温！");
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}
							}else{
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
								if(oVal !== "" && oVal < upVal){
									alert("最低气温不能大于最高气温！");
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}	
							}
							if(COMMON_TOP && upVal != old){
								var flag = COMMON_TOP.validateTemp(field, upVal);
								if(!flag){
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}
							}
							gridNumBoxObj.numberbox("setValue", upVal);
						} else {
							var getVal = gridNumBoxObj.numberbox("getValue") == "" ? 0 : parseFloat(gridNumBoxObj.numberbox("getValue"));
							var downVal =  getVal - 1;
							var old = gridNumBoxObj.next().find(".textbox-value").val();
							var field = gridNumBoxObj.closest(".nrietgrid-cell-editing").attr("field");
							var oVal = "";
							if(field.indexOf("MAX") > -1){
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
								if(oVal !== "" && oVal > downVal){
									alert("最高气温不能小于最低气温！");
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}
							}else{
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
								if(oVal !== "" && oVal < downVal){
									alert("最低气温不能大于最高气温！");
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}	
							}
							if(COMMON_TOP && downVal != old){
								var flag = COMMON_TOP.validateTemp(field, downVal);
								if(!flag){
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}
							}
							gridNumBoxObj.numberbox("setValue", downVal);
						}
					}
					if(e.detail) {
						if(e.detail > 0) {
							var getVal = gridNumBoxObj.numberbox("getValue") == "" ? 0 : parseFloat(gridNumBoxObj.numberbox("getValue"));
							var upVal =  getVal + 1;
							var old = gridNumBoxObj.next().find(".textbox-value").val();
							var field = gridNumBoxObj.closest(".nrietgrid-cell-editing").attr("field");
							var oVal = "";
							if(filed.indexOf("MAX") > -1){
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
								if(oVal !== "" && oVal > upVal){
									alert("最高气温不能小于最低气温！");
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}
							}else{
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
								if(oVal !== "" && oVal < upVal){
									alert("最低气温不能大于最高气温！");
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}	
							}
							if(COMMON_TOP && upVal != old){
								var flag = COMMON_TOP.validateTemp(field, upVal);
								if(!flag){
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}
							}
							gridNumBoxObj.numberbox("setValue", upVal);
						} else {
							var getVal = gridNumBoxObj.numberbox("getValue") == "" ? 0 : parseFloat(gridNumBoxObj.numberbox("getValue"));
							var downVal =  getVal - 1;
							var old = gridNumBoxObj.next().find(".textbox-value").val();
							var field = gridNumBoxObj.closest(".nrietgrid-cell-editing").attr("field");
							var oVal = "";
							if(field.indexOf("MAX") > -1){
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MIN']").text();
								if(oVal !== "" && oVal > downVal){
									alert("最高气温不能小于最低气温！");
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}
							}else{
								oVal = $(container).closest(".nrietgrid-row").find("td[field$='MAX']").text();
								if(oVal !== "" && oVal < downVal){
									alert("最低气温不能大于最高气温！");
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}	
							}
							if(COMMON_TOP && downVal != old){
								var flag = COMMON_TOP.validateTemp(field, downVal);
								if(!flag){
									gridNumBoxObj.next().find(".textbox-value").val(old ? old : "");
									return;
								}
							}
							gridNumBoxObj.numberbox("setValue", downVal);
						}
					}

					e.stopPropagation();
				};

				if(document.addEventListener) {
					document.addEventListener("DOMMouseScroll", scrollfun, false);
				}

				window.onmousewheel = document.onmousewheel = scrollfun;
				return gridNumBoxObj;
			},
			destroy: function(target) {
				$(target).numberbox("destroy");
				if(document.addEventListener) {
					document.addEventListener("DOMMouseScroll", function() {}, false);
				}
				window.onmousewheel = document.onmousewheel = function() {};
			},
			getValue: function(target) {
				var $tar = $(target).closest("div.nrietgrid-editable").find("span[name=\"cellhtm\"]").html();
				return $.isNumeric($tar) ? parseFloat($tar) : $tar;
			},
			setValue: function(target, value) {
				//		    	$(target).closest("div.nrietgrid-editable").attr({"oldval" : value});
				//		    	$(target).closest("div.nrietgrid-editable").find("span[name=\"cellhtm\"]").html(value); 
				$(target).numberbox("setValue", value && value != "" && value != "-" ? parseFloat(value) : value);
			},
			resize: function(target, width) {
				var targetObj = $(target);
				if($.boxModel == true) {
					targetObj.css({ "width": width - (targetObj.outerWidth() - targetObj.width()) + "px" });
				} else {
					targetObj.css({ "width": width + "px" });
				}
			}
		},
		ntext: {

		}
	};

	function getTop(element) {
		var top = element.offsetTop;

		if(!$(element.offsetParent).parent().hasClass("nrietpanel nrietgrid")) {
			top += getTop(element.offsetParent)
		} else {
			top += element.offsetParent.offsetTop;
		}
		return top;
	}

	function getLeft(element) {
		var left = element.offsetLeft;
		if(!$(element.offsetParent).parent().hasClass("nrietpanel nrietgrid")) {
			left += getLeft(element.offsetParent)
		} else {
			left += element.offsetParent.offsetLeft;
		}
		return left;
	}

	function getPosition(td) {
		var col = $(td).prevAll().length + 1 + sumpos($(td));
		var row = $(td).parent().prevAll().length + 1;
		return {
			row: row,
			col: col
		};
	}

	function sumpos(obj) {
		var cols = 0;
		if(null != obj.prev() && obj.prev().length > 0) {
			cols += obj.prev().eq(0).attr("colspan") ? obj.prev().eq(0).attr("colspan") - 1 : 0;
			if(null != obj.prev().eq(0).prev() && obj.prev().eq(0).prev().length > 0) {
				cols += sumpos(obj.prev().eq(0));
			}
		}
		return cols;
	}

	NrietGrid.defaults = {
		frozenColumns: null,
		columns: null,
		fitColumns: false,
		toolbar: null,
		striped: false,
		method: "post",
		nowrap: true,
		idField: null,
		url: null,
		loadMsg: "Loading...",
		rownumbers: false,
		cellSelect: false,
		singleSelect: false,
		pagination: false,
		pageNumber: 1,
		pageSize: 10,
		pageList: [10, 20, 30, 40, 50],
		queryParams: {},
		sortName: null,
		sortOrder: "asc",
		remoteSort: true,
		showHeader: true,
		showFooter: false,
		scrollbarSize: 18,
		rowStyler: function(_1d1, _1d2) {},
		loadFilter: function(data) {
			if(typeof data.length == "number" && typeof data.splice == "function") {
				return { total: data.length, rows: data };
			} else {
				return data;
			}
		},
		editors: editorsObj,
		editConfig: {
			getTr: function(target, index) {
				return $.data(target, "nrietgrid").panel.find("div.nrietgrid-body tr[nrietgrid-row-index=" + index + "]");
			},
			getRow: function(target, rowIndex) {
				return $.data(target, "nrietgrid").data.rows[rowIndex];
			}
		},
		view: renderView,
		headrowstyler: function(index, row) {},
		onBeforeLoad: function(param) {},
		onLoadSuccess: function() {},
		onLoadError: function() {},
		onClickRow: function(rowIndex, rowData) {},
		onDblClickRow: function(rowIndex, rowData) {},
		onClickCell: function(rowIndex, filed, value) {},
		onDblClickCell: function(rowIndex, filed, value) {},
		onSortColumn: function(sort, order) {},
		onResizeColumn: function(filed, width) {},
		onSelect: function(rowIndex, rowData) {},
		onUnselect: function(rowIndex, rowData) {},
		onSelectCell: function(rowIndex, rowData, field) {},
		onUnSelectCell: function(rowIndex, rowData, field) {},
		onSelectAll: function(rows) {},
		onUnselectAll: function(rows) {},
		onBeforeEdit: function(rowIndex, rowData) {},
		onAfterEdit: function(rowIndex, rowData, changes) {},
		onCancelEdit: function(rowIndex, rowData) {},
		onHeaderContextMenu: function(e, filed) {},
		onRowContextMenu: function(e, rowIndex, rowData) {},
		onSelectColumn: function(field, colData) {},
		onUnselectColumn: function(field, colData) {}
	};

	function checkElemArrHasId(elemArr, id) {
		if(isEmpty(elemArr) || isEmpty(id)) {
			return false;
		}
		for(var i = 0; i < elemArr.length; i++) {
			var elem = elemArr[i];
			if(isEmpty(elem) || isEmpty(elem.id)) {
				continue;
			}
			if(elem.id == id) {
				return true;
			}
		}
		return false;
	}

	function checkElemArrHasClass(elemArr, cls) {
		if(isEmpty(elemArr) || isEmpty(cls)) {
			return false;
		}
		for(var i = 0; i < elemArr.length; i++) {
			var elem = elemArr[i];
			if(isEmpty(elem) || isEmpty(elem.className)) {
				continue;
			}
			if(hasClass(elem, cls)) {
				return true;
			}
		}
		return false;
	}

	function checkElemArrHasElem(elemArr, elem) {
		if(isEmpty(elemArr) || isEmpty(elem)) {
			return false;
		}
		for(var i = 0; i < elemArr.length; i++) {
			var _elem = elemArr[i];
			if(isEmpty(_elem)) {
				continue;
			}
			if(_elem == elem) {
				return true;
			}
		}
		return false;
	}

	function isEmpty(param) {
		if(param == undefined || param == null || (typeof param == "string" && param == "")) {
			return true;
		}
		return false;
	}

	function hasClass(elem, cls) {
		if(typeof elem.className !== "string") {
			return false;
		}
		return elem.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
	}
	CafeJS.ngrid = function(settings) {
		var $ngrid = new NrietGrid(settings);
		$ngrid.target = $(settings.gridRender);
		if($ngrid.options().fit) {
			var p = $ngrid.getPanel().closest("div.nrietpanel.nrietgrid").parent();
			p.resize(function() {
				//				console.log(p.width());
				$ngrid.resize({ "width": p.width() });
			});
		}
		top.document.onclick = document.onclick = function(event) {
			if(isEmpty(event) || isEmpty(event.path)) {
				return;
			}
			var path = event.path;
			if(!checkElemArrHasId(path, "settings.gridRender") &&
				!checkElemArrHasClass(path, "nrietpanel") &&
				!checkElemArrHasClass(path, "nrietgrid") &&
				!checkElemArrHasClass(path, 'tab_box')) {
				var elem = $(document).find("div.nrietgrid");
				if(!isEmpty(elem)) {
					$.each(elem, function(i, j) {
						clearEdit($(j).find("div[id]:hidden")[0]);
					})
				}
			}
		}
		//		console.log($ngrid.options());
		return $ngrid;
	};
});